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

Ligonsker's avatar

Route Middleware group and Prefix group - which is the the better syntax?

There are a few options to group routes:

Route::middleware(['first', 'second'])->group(function () {

or

Route::group(['middleware' => 'some_middleware']), (function () {

And then I also want to add the prefix:

Route::prefix('admin')->group(function () {

So what would be the preferred way to both have middleware and prefix on a group of routes? For example these 2:

    Route::get('/orders/{id}', 'show');
    Route::post('/orders', 'store');
0 likes
6 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The second method is the "old" method. I would use the modern one

Route::prefix('admin')->middleware(['first', 'second'])->group(function () {
    Route::get('/orders/{id}', 'show');
    Route::post('/orders', 'store');
});
1 like
Ligonsker's avatar

@Sinnbeck Btw, is it possible to nest Middleware groups like so: (so that the last group in this example has both first, second and also its the only one that has third

Route::prefix('admin')->middleware(['first', 'second'])->group(function () {
    Route::get('/orders/{id}', 'show');
    Route::post('/orders', 'store');
    
    //... more routes ...

    Route::middleware(['third'])->group(function () {
        Route::get('/some_route', 'show');
        Route::post('/some_route', 'store');
    });
});
Sinnbeck's avatar

@Ligonsker Yeah you are free to nest as much as you wish :) I do this myself. The same can be done with prefixes

Route::prefix('admin')->middleware(['first', 'second'])->group(function () {
     Route::prefix('orders')->middleware(['third'])->group(function () {
        Route::get('{id}', 'show');
        Route::post('', 'store');
    });
});
1 like

Please or to participate in this conversation.