How to create custom route file in laravel

If you are working with a large application in Laravel and you have different types of users then you have to create custom route files for user type in your application. for example, if you have user, manager, and admin three types of users then you have different prefixes like “user/“, “admin/“, and “/*” URL will be. so if you create different route files then you can easily make it the better way your routes.

On working enterprise applications where the area of work is much bigger. It’s idle to split resources and make code more understandable to other developers then splitting routes files are best practice for it. In Laravel, we can create custom route files as per our requirements by simply following the below steps:

You can use an existing Laravel application or create a new one for this. Let’s start with our example.

Create Custom Route Files

First of all, let’s create separate route files for the admin and user. so let’s create a route file.

  1. Guest Routes: For Guest routes, you will use the default web.php file.
  2. Admin Routes: You have to create a new file for admin routes in the route folder. This file will handle all routes for admin.
  3. User Routes: Here you have to create a new file user.php in the routes folder. in that file, you have declared routes for the user.

We will create all new route files in the routes\ directory. Let’s create files one by one

routes\web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    echo "Welcome to Codewolfy";
});
Route::get('/page1', function () {
    echo "Page 1";
});
Route::get('/page2', function () {
    echo "Page 2";
});

This file has only three routes. This means guest users can only access /, /page1, and /page2.

admin\admin.php

<?php
Route::get('admin/', function () {
    echo "Welcome Admin to Codewolfy";
});
Route::get('admin/page1', function () {
    echo "Admin Page 1";
});

admin\user.php

<?php
Route::get('user/', function () {
    echo "Welcome User to Codewolfy";
});
Route::get('user/page1', function () {
    echo "User Page 1";
});

Now our route files are created. In the next step, we have to bind it to our application.

Add Files to the Service Provider

In this step, we have to link our newly created route files to our application using RouteServiceProvide. RouteServiceProvider handles all routing-related services.

So, let’s open RouteServiceProvider.php and make the following changes to 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
{
    public const HOME = '/home';

    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'));

        });
    }

    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

Now, we are ready to test our application with custom route files.

Testing Our Application

php artisan serve

Open the below URL in your browser:

http://127.0.0.1:8000/*
http://127.0.0.1:8000/user/*
http://127.0.0.1:8000/admin/*

Here, you can use all route functions and functionality on those files like route group, prefix, assigning middleware, and much more.