Laravel 11: Multiple route files

With Laravel 11, the internal team has streamlined the application structure, particularly in how routes are organized and managed. This change is designed to simplify the routing setup, making it more intuitive and easier to maintain as applications grow in complexity.

What’s New with Route Files in Laravel 11?

In earlier versions of Laravel, adding new route files involved customizing the RouteServiceProvider.php file. While effective, this approach required developers to dive into the service provider to define any additional routes beyond the defaults (such as web.php, api.php, etc.). Laravel 11 introduces a more organized approach by moving route configuration to a single location, allowing developers to add new route files by modifying bootstrap/app.php instead of the service provider.

The Simpler Way: Using bootstrap/app.php

In Laravel 11, routing configurations have shifted from RouteServiceProvider.php to bootstrap/app.php. This file now plays a more central role in defining and including various route files, keeping all route definitions accessible in one place.

To add a new route file in Laravel 11, you simply include it in bootstrap/app.php. This setup aims to reduce the steps needed to configure new routes, making the structure clearer and more modular.

Why the Change?

This update reflects a broader philosophy in Laravel’s evolution: to make configuration and maintenance simpler for developers, especially when scaling applications. By consolidating routing setup into bootstrap/app.php, the Laravel team has reduced the complexity associated with adding and managing routes, especially when working in large teams or on projects with extensive routing needs.

Practical Example: Adding a New Route File in Laravel 11

Let’s say you want to add a custom route file called admin.php for admin-related routes:

  1. Create the new route file in the routes directory. For example: routes/admin.php.

  2. Register the new route file in bootstrap/app.php:

1
2
3
4
5
6
7
8
9
10
11
12
...
->withRouting(
...
then: function($router){
// Admin
Route::prefix('adm')
->middleware(['web'])
->name('adm.')
->group(base_path('routes/admin.php'));
}
)
...

And that’s it! Laravel will now load admin.php alongside other route files without needing additional configuration in RouteServiceProvider.php.

Breaking Down the Code

Let’s go over each component used here:

  1. Prefix: Route::prefix('adm')
  • This line specifies a prefix for all routes in admin.php, so each route under this group will start with /adm. For example, if admin.php contains a route like /dashboard, it would be accessible at /adm/dashboard.

  • Prefixes are useful when you want to logically group routes under a specific URL segment, making the structure of your routes more consistent. In this case, the prefix adm keeps all admin routes under a distinct namespace, which helps both organization and security.

  1. Middleware: ->middleware(['web'])
  • This line applies middleware to all routes in the group. Here, the web middleware is applied, which is often used for routes that need session state, CSRF protection, and other web-specific features.

  • Middleware provides a way to apply shared behavior to a group of routes, such as authentication or logging, without having to add it individually to each route. This makes it easier to enforce policies on specific groups of routes—here, ensuring that all admin routes have the web middleware.

  1. Name: ->name('adm.')
  • This sets a naming prefix for all routes in the group, so each route within admin.php will have a name that starts with adm.. For instance, if there’s a route named dashboard in admin.php, its full route name would be adm.dashboard.

  • Naming prefixes are helpful for generating URLs or redirecting within the application. By adding a prefix, you can ensure all admin route names are unique and clearly identified, making it easier to manage them in your code.

  1. Group File: ->group(base_path('routes/admin.php'))
  • This groups all routes within admin.php under the specified prefix, middleware, and name settings.

  • Using ->group() allows you to organize and manage routes within a specific file, keeping the main routing logic in bootstrap/app.php clean and modular.

Why Use Multiple Route Files?

In large applications, using multiple route files provides several advantages that help keep your code organized, modular, and easier to maintain. Here’s why:

  1. Separation of Concerns: Each route file can be dedicated to a specific domain or functionality within your application. For example, you might separate routes for user management (user.php), admin-specific functionality (admin.php), and API endpoints (api.php). This separation keeps each route file focused on one area, making it easier for developers to find and manage specific routes.

  2. Enhanced Collaboration: With multiple route files, teams working on different features or areas of an application can focus on their relevant routes without affecting unrelated parts of the code. This separation reduces conflicts when merging code changes and improves team productivity.

  3. Improved Readability: As applications grow, routing can become complex and hard to navigate. By categorizing routes into different files, developers can quickly understand the structure of the application, identify where certain routes are defined, and avoid scrolling through lengthy files.

  4. Better Testing and Debugging: Debugging becomes more efficient when routes are segmented. If there’s an issue with a specific functionality, developers can focus on the related route file instead of sifting through an all-encompassing list of routes.

  5. Scalability: For applications with microservices or modular components, having dedicated route files for different parts of the system supports a scalable architecture. This modular approach aligns with modern development practices, allowing routes to be modified or extended without disrupting other parts of the application.

By using multiple route files, Laravel applications can achieve a cleaner structure, improve team productivity, and support long-term growth. Laravel 11’s improved routing organization facilitates this approach, making it easier to implement and manage multiple route files efficiently.