Once your Laravel application grows larger, you certainly want to organize all the routes into smaller files instead of using one large web.php
or api.php
files. In this tutorial, I will show you how to create custom route files in Laravel 9.
Article Contents
Pre-requisites
We will start a fresh new Laravel 9 project for this tutorial, make sure you have PHP v8.0.2+ installed in your development environment.
Use the following command to create the project:
$ composer create-project laravel/laravel custom-route-files
Make sure everything works correctly by start local Laravel development server.
$ cd custom-route-files
$ php artisan serve
Starting Laravel development server: http://127.0.0.1:8000
PHP 8.1.5 Development Server (http://127.0.0.1:8000) started
Open the link http://127.0.0.1:8000
on browser to confirm.
It’s ready so let’s get started!
Route files location
By default, all the routes are defined under the routes/*.php
, if you take a look at the directory structure, it will be like this:
routes
├── api.php
├── channels.php
├── console.php
└── web.php
As you might already know, the frontend routes are defined inside of these two:
api.php
: contains all routes for API endpoints.web.php
: contains all routes for content pages.
When your Laravel application grows larger, the content of those two files will get heavy and hard to manage, and you will want to break them down into smaller files. Yeah, that’s what we’re going to do now.
Let say, we want to breakdown split all (regular) user-related web routes and admin-related web routes into two files:
web.php
: for user-related web routes.admin.php
: for admin-related web routes.
Since web.php
is already existed, we will create a new file routes/admin.php
, then put some routes in there.
Define the routes & controllers
We will define the routes for both routes/web.php
and routes/admin.php
like below:
// file: routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::prefix('/users')->group(function () {
Route::get('/dashboard', [\App\Http\Controllers\UserController::class, 'showDashboard']);
Route::get('/settings', [\App\Http\Controllers\UserController::class, 'showSettings']);
});
// file: routes/admin.php
<?php
use Illuminate\Support\Facades\Route;
Route::prefix('/admin')->group(function () {
Route::get('/dashboard', [\App\Http\Controllers\AdminController::class, 'showDashboard']);
Route::get('/settings', [\App\Http\Controllers\AdminController::class, 'showSettings']);
});
For the simplicity of the tutorial, other things like authentication, middleware..are omitted.
We also need to create two corresponding controllers mentioned in the route files, UserController
and AdminController
by executing following commands:
$ php artisan make:controller UserController
$ php artisan make:controller AdminController
and update the controllers’ content:
// file: app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function showDashboard()
{
return 'User Dashboard';
}
public function showSettings()
{
return 'User Settings';
}
}
// file: app/Http/Controllers/AdminController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function showDashboard()
{
return 'Admin Dashboard';
}
public function showSettings()
{
return 'Admin Settings';
}
}
Register route files
After creating route files and the associating controllers, we need to register the route files, which should be done by updating the RouteServiceProvider
file.
By default, this is what it looks like in app/Providers/RouteServiceProvider.php
, and take a look at the boot()
method:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
});
}
This boot()
method defines how we register a route file into the Laravel application.
In our case, we need to register the custom routes/admin.php
, so the update code will look like this:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
// add this following line to register `routes/admin.php`
Route::middleware('web')
->group(base_path('routes/admin.php'));
});
}
Verify the routes
Once the route files are registered properly, you can restart the local Laravel dev server and confirm the routes on browser. These following routes should show corresponding content when you access:
http://127.0.0.1:8000/admin/dashboard
http://127.0.0.1:8000/admin/settings
http://127.0.0.1:8000/user/dashboard
http://127.0.0.1:8000/user/settings
Conclusion
All done! That’s how you create custom route files in Laravel 9 application. This simple technique will help you easily manage all the routes in your Laravel application.
For reference, you can visit the project demo code on Github by clicking below link: