Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 378
Lucha Laura Hardie Avatar asked Mar 05 '15 18:03

Lucha Laura Hardie


People also ask

Can we apply two middleware on a single route?

To assign middleware to a route you can use either single middleware (first code snippet) or middleware groups (second code snippet). With middleware groups you are assigning multiple middleware to a route at once. You can find more details about middleware groups in the docs.

Can we use multiple middleware?

We can use more than one middleware on an Express app instance, which means that we can use more than one middleware inside app.

How do you use middleware on a route?

Assigning Middleware To Routes If you would like to assign middleware to specific routes, you should first assign the middleware a key in your application's app/Http/Kernel.php file. By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel.

What are the two main routing files found in Laravel?

The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.


2 Answers

This error Route [admin] not defined is because of the route name admin is not defined.

Route name and Route path are two different things.

And you've declared the route path as admin,

Route::get('admin', 'DashboardController@index');

However,

return redirect()->route('admin'); 

means that you are redirecting the flow to the route named admin.

To sort the error,

Define a route name admin as follows in an array defined below with 'as' => 'route_name'.

Solution :

Route::get('admin', [
   'as' => 'admin',
   'uses' => 'DashboardController@index'
]);

Please refer the link : https://laravel.com/docs/master/routing#named-routes

like image 151
Aniruddha Shevle Avatar answered Jan 03 '23 12:01

Aniruddha Shevle


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'
]
like image 43
Vincent Bouchard Avatar answered Jan 03 '23 12:01

Vincent Bouchard