How to Create Custom Route File in Laravel 8

Share this Article
Reading Time: 5 minutes
2,284 Views

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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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

Open project to terminal and type the command 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.

Find More on Laravel 8 Articles here

Leave a Comment