Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

How to create multiple routes file in Laravel 8

How to create multiple route files in Laravel 8?

In this tutorial, I will show you an example of how to create multiple route files in the Laravel 8 application. you can understand the concept of laravel adding a custom route file. I will explain to you step by step how to add custom route files in laravel. I will explain how to create a custom route file in laravel. You just need some steps to define the custom route file in laravel 5, laravel 6, laravel 7, laravel 8, and laravel 9.

First, what we’re doing here, This is the example :

create multiple route files in Laravel 8
custom admin route file in laravel

custom user route file in laravel

Need to create multiple route files in Laravel

If you are working with a giant application like e-commerce or Erp projects in laravel and you have different types of users then you have to create a custom route file for user type-wise in your application. for example if you have user, admin, and frontend three types of the user then you have a different prefix like “user/“, ” and “admin/ URL will be. so if you create a different route file then you can easily make it the better way your routes.

Steps to create a custom route file in laravel 8

In this example, I will let you know how you create a custom route file in the laravel application. you can easily use it with laravel 5, laravel 6, laravel 7, laravel 8, and laravel 9. So let’s follow the below steps:

In this tutorial, I am going to share with you how to define and use subdomain routes better way in the Laravel 8 application.

Create new custom route files

The first step is to create route custom files under the routes folder in the laravel application.

  • admin.php
  • user.php
define custom route files in laravel

Register your custom Routes

In this step, we will register our custom route files in the RouteServiceProvider file.

app\Providers\RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    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'));
        });

         Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/admin.php'));

         Route::middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/user.php'));
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

Now we test our custom route file in the Laravel application

Create two controllers for the user and admin.

php artisan make:controller Admin\AdminController
php artisan make:controller User\UserController

routes\admin.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AdminController;

/*
|--------------------------------------------------------------------------
| Admin Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/admin', [AdminController::class, 'adminRoutes']);

routes\user.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\User\UserController;

/*
|--------------------------------------------------------------------------
| User Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/user', [UserController::class, 'userRoutes']);

app\Http\Controllers\Admin\AdminController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function adminRoutes(){
        dd('This is a Admin Routes');
    }
}

app\Http\Controllers\User\UserController.php

<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function userRoutes(){
        dd('This is a User Routes');
    }
}

Run application :

http://127.0.0.1:8000/admin
custom-admin-route-file
http://127.0.0.1:8000/user
custom-user-route-file

In this article, we learned “How to create multiple route files in Laravel 8”, I hope this article will help you with your Laravel application Project.

Read also: hasMany Relation with where condition in Laravel.