3

Is there a way to get the parent route group of current route in Laravel middlewares?

public function handle($request, Closure $next, $guard = null)
{
    // here I mean
}
1
  • Please give an example of what your routes are and what you expect to get as an end result. Commented Aug 10, 2016 at 10:42

1 Answer 1

3

You can't get the group from a route, especially that you can nest groups within other groups.

The easiest way to be able to tell which group current request belongs to would be to add a parameter to your middleware - see the docs for more details here: https://laravel.com/docs/5.2/middleware#middleware-parameters

First, declare the parameter in your handle() method:

public function handle($request, Closure $next, $group = null)
{
  // your logic here
}

Then, when assigning middleware to groups, you can pass group name as a parameter:

Route::group(['middleware' => 'yourmiddleware:groupname'], function () {
  // your routes go here
});

Then in your middleware you can do different actions based on the group name passed:

if ($group === 'groupname') {
  // some custom logic here
}

If the logic you plan to do for different groups is different you should consider implementing multiple middlewares instead of passing group name as a parameter just to keep responsibilities separated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.