Arranging Many Laravel Routes in Separate Files

Routing

Committed to the LaraBrain by: kohenkatz at October 27, 2015 1:11 am

Make it easy to organize and find your Route declarations for different parts of your site

Many large sites have multiple different areas with different functions, and lots of different pages within each area. By default, Laravel uses a single routes.php file in the app/Http directory in which all of the Routes are defined. This leads to long routes.php files with lots of Route::group calls:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
    // Lots of routes here
});

Route::group(['prefix' => 'user', 'namespace' => 'User'], function () {
    // Lots of routes here
});

Route::group(['prefix' => 'status', 'namespace' => 'Status'], function () {
    // Lots of routes here
});

Route::group(['prefix' => 'api', 'namespace' => 'Api'], function () {
    // Lots of routes here
});

Instead, we can break up the routes.php file into several separate files, one for each area of the site.

To start, create a directory named routes inside the app/Http directory (or anywhere else you'd like).

Inside the new folder, create a file for each area of your site. (For the example above, create admin.php, user.php, status.php, and api.php.)

In each of the new files, put in the Route::method calls for that section. Do not put in the Route::group call!

Open the app/Providers/RouteServiceProvider.php file. You should see something like the following:

 <?php

 // ... SNIP

 class RouteServiceProvider extends ServiceProvider
 {
     protected $namespace = 'App\Http\Controllers';

     // ... SNIP

     public function map(Router $router)
     {
         $router->group(['namespace' => $this->namespace], function ($router) {
             require app_path('Http/routes.php');
         });
     }
 }

Replace the single require call with your Route::group calls and require statements. For my example above, your file will look like this:

 <?php

 // ... SNIP

 class RouteServiceProvider extends ServiceProvider
 {
     // ... SNIP

     public function map(Router $router)
     {
         $router->group(['namespace' => $this->namespace], function ($router) {
             $router->group(['prefix' => 'admin', 'namespace' => 'Admin'], function($router) {
                 require app_path('Http/routes/admin.php');
             });
             $router->group(['prefix' => 'user', 'namespace' => 'User'], function($router) {
                 require app_path('Http/routes/user.php');
             });
             $router->group(['prefix' => 'status', 'namespace' => 'Status'], function($router) {
                 require app_path('Http/routes/status.php');
             });
             $router->group(['prefix' => 'api', 'namespace' => 'Api'], function($router) {
                 require app_path('Http/routes/api.php');
             });
         });
     }
 }

This technique works equally well for splitting up the routes for subdomain routing:

 ```
 <?php

 // ... SNIP

 class RouteServiceProvider extends ServiceProvider
 {
     protected $namespace = 'App\Http\Controllers';

     // ... SNIP

     public function map(Router $router)
     {
         $router->group(['namespace' => $this->namespace], function ($router) {
             $router->group(['domain' => 'admin.example.com', 'namespace' => 'Admin'], function($router) {
                 require app_path('Http/routes/admin.php');
             });
             $router->group(['domain' => 'user.example.com', 'namespace' => 'User'], function($router) {
                 require app_path('Http/routes/user.php');
             });
             $router->group(['domain' => 'status.example.com', 'namespace' => 'Status'], function($router) {
                 require app_path('Http/routes/status.php');
             });
             $router->group(['domain' => 'api.example.com', 'namespace' => 'Api'], function($router) {
                 require app_path('Http/routes/api.php');
             });
         });
     }
 }
 ```