Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ronnyandre's avatar

Combined route group with prefix and auth middleware?

I have a route group with admin URL prefix, that looks like this:

Route::group( [ 'prefix' => 'admin' ], function()
{
    Route::get('dashboard', function() {} );
    Route::get('settings', function() {} );
});

But I want to combine this with using the auth middleware, but this didn't work:

Route::group( [ 'prefix' => 'admin' ], [ 'middleware' => 'auth', function()
{
    Route::get('dashboard', function() {} );
});

What is the correct way to do this? Do I have to do two route groups?

0 likes
10 replies
pmall's avatar
pmall
Best Answer
Level 56
Route::group(['prefix' => 'admin',  'middleware' => 'auth'], function()
{
    Route::get('dashboard', function() {} );
});
30 likes
ronnyandre's avatar

Thanks for your quick reply!

I thought I already tried this, but I guess I was wrong, because this worked like a charm... Thanks @pmall !

ABCrafty's avatar

thank you for this answer, helped me a lot for my first admin panel :D

highnoon's avatar

To add more than 1 middleware

 Route::group([
            'prefix'     => 'admin',
            'middleware' => [
                'auth',
                'anotherMiddleware',
                'yetAnotherMiddleware',
            ],
        ], function() {
            
           Route::get('dashboard', function() {} );
 });
8 likes
highnoon's avatar

You can also add Middleware Groups, defined in Kernel.php

just add another group below 'web'

/**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    
    //custom middleware group
      'customMiddlewareGroup' => [
           
        ],
        'api' => [
            'throttle:60,1',
            'bindings',
        ],

    ];
lbottoni's avatar

grazie tykus, i had already read this document, but for understand, the "attributes" are they the methods of Route?

example Route::group(["prefix"=>...,"middleware"=>..,"domain"=>...,"match"=>...],function(){ })

"Shared attributes are specified in an array format as the first parameter to the Route::group method"

1 like

Please or to participate in this conversation.