Support the ongoing development of Laravel.io →
Security Requests IOC

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.

Last updated 3 years ago.
0

It actually works. I though auth was already under the web group which is not.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

hcancelik hcancelik Joined 3 Jul 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.