I have an additional middleware registered as "admin" which checks if the user is admin.
public function handle($request, Closure $next)
{
if(!$request->user()->isAdmin()){
return redirect('/')->withErrors(['Cannot go there']);
}
return $next($request);
}
and let's say that here is a part of routes.php:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'FrontendPageController@index')->name('home');
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () {
Route::get('/', 'Admin\BackendPageController@index');
Route::get('/help', 'Admin\BackendPageController@showHelp');
}];
}];
How can I make sure the auth middleware under web middleware group runs before the admin middleware? Right now I get an error when a user is not logged in and tries to go to an admin page. Since there is no user instance I'm calling isAdmin() on null. I can just another check into if statement to see if there is a user instance . However is there a way to specify which middleware should run first. I thought grouping routes under a group will be inherited so in the example below it would be "web" group first and then the "admin" but it seems that's not the case.
It actually works. I though auth was already under the web group which is not.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community