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

ethar's avatar
Level 5

laravel 11 create custom route file

i want to create custom route file for prefix admin, i edited bootstrap/app.php

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            Route::namespace('Admin')
                ->prefix('admin')
                ->name('admin.')
                ->group(base_path('routes/admin.php'));
        },
    )

and create routes/admin.php

Route::group(['prefix' => 'admin', 'middleware' => 'guest:web', 'namespace' => 'Admin'], function () {
    Route::get('/login', [AuthController::class, 'index'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('admin.login');
});

Route::group(['prefix' => 'admin', 'middleware' => ['auth:web'], 'namespace' => 'Admin'], function () {
    Route::get('/switcher', function () {
        return view('Admin.config.switcher');
    })->name('switcher');
    Route::get('/logout', [AuthController::class, 'logout'])->name('admin.logout');
    Route::get('/', [DashboardController::class, 'index']);
    Route::get('/developer', [DashboardController::class, 'index'])->name('about.developer');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index');


});

but not working

0 likes
2 replies
s4muel's avatar
s4muel
Best Answer
Level 50

what does it mean "not working"? seems fine, just one thing is, that you define the prefix twice. one here in bootstrap/app.php:

...
then: function () {
    Route::namespace('Admin')
        ->prefix('admin') //<- here
        ->name('admin.')
        ->group(base_path('routes/admin.php'));
},

and here too in routes/admin.php:

Route::group(['prefix' => 'admin', 'middleware' => 'guest:web', 'namespace' => 'Admin'] //<- here and below in your code for the second group too

the links then will be like this:

/admin/admin/login
/admin/admin/logout
/admin/admin/developer
...

what it the output when you run php artisan route:list command?

NEWA's avatar

If you want to keep only admin routes in a different file, you can create a file named admin.web.php inside the routes folder.

Then, you can add the following line to the web.php file:

require base_path('/routes/admin.web.php');

Please or to participate in this conversation.