How to Add More than One Item to Middleware on Route in Laravel 5

Lucha Laura Hardie picture Lucha Laura Hardie · Mar 5, 2015 · Viewed 9.9k times · Source

I recently started using Laravel 5 and I'm having a lot of trouble implementing a system that not only authorizes users, but also checks permissions.

In all of the examples I've dug up online, I see two items being applied as middleware. For example:

Route::group(['middleware' => ['auth', 'permissions']], function() {
  // protected routes here
  Route::get('admin', 'DashboardController@index');
});

However, I cannot get this to work no matter what I do. I can only apply one item as middleware, such as:

Route::group(['middleware' => 'auth'], function() {
  // protected routes here
  Route::get('admin', 'DashboardController@index');
});

If I apply two, I get the error "Route [admin] not defined."

I have tried everything I can think of, and I am banging my head against a brick wall. How on earth can I apply two or more items of middleware to one route?

Answer

Vincent Bouchard picture Vincent Bouchard · Jun 17, 2015

You might juste try to create one middleware doing more than on verification ?

In your Kernel.php you might have something like :

protected $routeMiddleware = [
    'auth' => 'Your\Route\Authenticate',
    'auth.permissions' => 'Your\Route\AuthenticateWithPermissions'
    'permissions' => 'Your\Route\RedirectIfNoPermissions'
]