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

Lars-Janssen's avatar

Handling huge route file

Hello,

My application has become bigger and bigger. Now my web.php has become huge. What is the best thing to do in this situation. Splitting it up?

Or how are you handling this?

Thanks a lot!

0 likes
2 replies
alenabdula's avatar

Can you define "huge"? How many lines of code, without comments, etc...?

I guess you can create php files in routes folder then just require them in your web.php file...

Screenbeetle's avatar

Glad it's not just me.

Since L5.3 you can split your routes into files. Since then I have separate route files relating to things like the auth levels and areas of the site. E.g

web.php //public routes
customer.php //customer login
admin.php // staff login

These are mapped in the RouteServiceProvider.php

public function map()
{
        
$this->mapWebRoutes();

$this->mapCustomerRoutes();
        
$this->mapAdminRoutes();

}

// then subsequently you can also add in things like a prefix to apply to all the routes in each file:

protected function mapCustomerRoutes()
{
Route::group([
       'middleware' => 'web',
       'prefix' => 'customer',
       'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/customer.php');
        });
}

It also makes it very clear and easy to do things like apply middleware and pass parameter to a set of routes e.g.

    protected function mapAdminRoutes()
    {
        Route::group([
        // pass admin param to role middleware to check admin privileges
            'middleware' => ['web', 'role:admin' ], 
            'prefix' => 'admin',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/admin.php');
        });
    }
1 like

Please or to participate in this conversation.