If we are building large project in web, then surely we have several routes. Do we think if we write all routes into a single file then it will be manageable, No it’s not. It will very tough to manage all routes from one place.
Inside this article, we will create custom route file in laravel 8. These routes files as per application modules.
This article is very interesting to learn. This will be step by step guide for the concept of Custom Route file in laravel tutorial.
To learn about Route group and prefix in laravel, Click here.
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
To start the development server of Laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Route Prefix Example
Let’s say we have an application in which we have modules as Admin, Customer.
If we write all routes all these given two modules into web.php, so it will look like something –
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\AdminController; use App\Http\Controllers\CustomerController; Route::get('/', function () { return view('welcome'); }); // Admin Routes Route::prefix("admin")->group(function(){ Route::get("create-user", [AdminController::class, "createUser"]); Route::get("list-users", [AdminController::class, "listUsers"]); Route::get("edit-user", [AdminController::class, "editUser"]); }); // Customer Routes Route::prefix("customer")->group(function(){ Route::get("list-purchase", [CustomerController::class, "listPurchase"]); Route::get("list-blogs", [CustomerController::class, "listBlogs"]); Route::get("create-blog", [CustomerController::class, "createBlog"]); }); //.. other module routes
For now, we have considered only two modules inside any application, but in case for 20 modules and their routes web.php file will be too much length and messy as well. We will not be able to manage entire application.
Here comes the concept of custom route file in laravel.
Custom Route Files
Create files as admin.php and customer.php as per needed modules into /routes folder.
Open /routes/admin.php, inside this file we will place all admin related routes.
<?php use Illuminate\Support\Facades\Route; // Admin Routes Route::prefix("admin")->group(function(){ Route::get("create-user", [AdminController::class, "createUser"]); Route::get("list-users", [AdminController::class, "listUsers"]); Route::get("edit-user", [AdminController::class, "editUser"]); });
Open /routes/customer.php, inside this file we will place all customer related routes.
<?php use Illuminate\Support\Facades\Route; // Customer Routes Route::prefix("customer")->group(function(){ Route::get("list-purchase", [CustomerController::class, "listPurchase"]); Route::get("list-blogs", [CustomerController::class, "listBlogs"]); Route::get("create-blog", [CustomerController::class, "createBlog"]); });
Successfully, we can see that we have separated each module routes into different different route files. One more important is that, now the application routes are mode readable.
Register Custom Route Files
Open RouteServiceProvider.php file from /app/Providers folder.
Inside this class, search for boot() method.
public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); // Admin Route file Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/admin.php')); // Customer Route file Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/customer.php')); }); }
Here, as we can see we are using the default middleware web to both route files admin.php and customer.php. In case if we have different middleware then we can pass and use it.
All we done !
Application Testing
Run this command into project terminal to start development server,
php artisan serve
Admin URLs – http://127.0.0.1:8000/admin/create-user and all.
Customer URLs – http://127.0.0.1:8000/customer/list-purchase and all.
We hope this article helped you to learn about How to Create Custom Route File in Laravel 8 in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.