How to Create Custom Route File in Laravel 8

Routes are entry points to a Laravel application. There are two route files available in Laravel 8 by default. These are called web.php and api.php.

If required one can create custom route file in Laravel easily.

In this article I am going to show you how to create Custom Route File in Laravel 8.

Perquisite: An installed and working Laravel Application

Essentially there are two methods to create a custom Laravel route file. I will show the first one in this post and second one later sometime. For ones with older version of Laravel, can try this link.

In this post I will demonstrate creating /admin routes here, for example calling http://127.0.0.1:8000/admin

Let’s start doing it now.

Create a file routes/admin.php with the following code:

use App\Http\Controllers\UsersController;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\Router;
use Illuminate\Http\Request;

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

Route::get('/', function() {
    print('I am an admin');
});

Next, open the app/Providers/RouteServiceProvider.php and find public function boot() in it. Add the following code to it, just above or below the existing lines of code for web and api routing.

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

So it becomes:
    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::prefix('admin')
                ->middleware(['web', 'admin'])
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));
        });
    }

You should see I am an admin message on your browser window at http://127.0.0.1:8000/admin.

Note: Clear cache and test the url if it does not update. You can create a simple create cache route in your web.php routes file as shown below:

Route::get('/clear-cache', function() {
    Artisan::call('optimize:clear');
    echo Artisan::output();
});

You can follow this article about clearing cache in Laravel application.

In next articles I will show you how to create a set of an Admin routes so you can login via web routes but still protect your admin area via custom user login access check. (Edit: It is already there, check it now!)

I hope you liked this article about Create Custom Route File!

One thought on “How to Create Custom Route File in Laravel 8

Leave a Reply

Your email address will not be published.