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?
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.
We can use more than one middleware on an Express app instance, which means that we can use more than one middleware inside app.
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.
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.
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
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'
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With