Laravel: Organizing your routes into several files

Not everything must be on a wall of routes in your routes file.

Italo Baeza
Jul 29 · 3 min read

Simple applications may use the routes/web.php or routes/api.php files to describe their routes. Since they’re small, there is no much to write and is sane to keep it under 50 or less.

Others, may have more than 50, even hundreds of routes. Instead of keep writing them inside a single file, you can organize your routes using multiple files. These are my top recommendations to deal with a slim and organized routes files so you don’t lose track of what and where.

Just add another file

Go to your App/Providers/RouteServiceProvider and find the map() method. Here the Service Provider will map your Routes. A quick glance on the file and you will note that API and Web routes are mapped using other methods.

You can just simply copy-paste the code inside mapWebRoutes() and change the group file for the one you want:

/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/my-custom-routes.php'));
}

The application will map the API routes, the Web routes, and finally your own custom routes.

Group a file

The Routes facade allows you to create a group of routes by using a Closure that returns more routes, but, as you saw earlier, the framework starts by calling a file.

In your routes files you can use the same technique. Instead of using a Closure, you can simply point to another file, effectively slimming down the route file.

Route::name('dashboard')
->prefix('dashboard')
->namespace('Dashboard')
->middleware(['auth','verified'])
->group(__DIR__ . '/web/dashboard.php');

Then, we can create a routes/web/dashboard.php file containing the routes for our Dashboard. We also apply a custom namespace, prefix, middleware for all routes and a name to be appended.

We can also nest more groups inside a group, and have multiple on one file. No problem:

// routes/web/dashboard.phpRoute::get('/', 'HomeController');
Route::get('/account', 'AccountController@show');
Route::post('/account', 'AccountController@update');
Route::name('users')
->prefix('users')
->namespace('Users')
->middleware('can:manage-users')
->group(__DIR__ . '/dashboard/users.php');
Route::name('delivery')
->prefix('delivery')
->namespace('Delivery')
->middleware('can:manage-delivery')
->group(__DIR__ . 'dashboard/delivery.php');

A simple group

Sometimes it’s not necessary to put names, prefixes, namespaces and middlewares to a group. You can simply create a group, and put all routes inside, and manage their prefixes, namespaces, middlewares and names by a case-by-case scenario. I will just spit something random just to make an example:

// routes/web.phpRoute::group(__DIR__ . '/web/message.php');// routes/web/message.phpRoute::get('client/send', 'MessageController@clientShow');
Route::post('client/send', 'MessageController@clientSend');
Route::get('courier/send', 'MessageController@courierShow')
->middleware('can:send-message-to-courier');
Route::get('courier/send', 'MessageController@courierShow')
->middleware('can:send-message-to-courier');

Route Model Binding as prefix

Another thing you can do is to create groups is use Route Model Binding as a prefix, making all your child routes share the same binding.

// routes/web.phpRoute::prefix('{delivery}')->group(__DIR__ . '/web/delivery.php');// routes/web/delivery.phpRoute::get('/', 'DeliveryController@show');
Route::get('/routes', 'DeliveryController@routes');
Route::get('/products', 'DeliveryController@products');
Route::get('/drivers', 'DriverController@deliveryDriver');

So there is no need to change literally puke walls of Routes into one file and hoping for the best while you need a map to figure out where the hell is that named route that is colliding with other. /rant

Italo Baeza

Written by

Graphic Designer. Web Developer Full Stack. Retired Tech & Gaming Editor.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade