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
}
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
}
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.