creating custom route file laravel

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

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

You can use existing Laravel application or create new one for this. Let's start our example.

Create Custom Route Files

First of all let's create separate route files for admin and user. so let's create route file.

  • Guest Routes : For Guest routes you will use default web.php file.
  • Admin Routes : You have to create new file for admin routes into route folder. This file will handle all routes for admin.
  • User Routes : Here you have to create new file user.php in routes folder. in that file you have declare routes for user.

We will create all new route files into 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";
});
    

In this file, it has only three routes. Which means guest user 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 next step we have to bind it to our application

Add Files to 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 following changes:

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
{
    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 below URL into 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 function and functionality on those files like, route group, prefix, assigning middleware and much more.


Share your thoughts

Ask anything about this examples