Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

How to Create Custom Route File in Laravel 11

Last Updated on April 16, 2024 by

In this example tutorial, I will show you how to create, configure and use a custom routes file with routes in a laravel 11 application.

Let’s learn how to create,configure and use a custom route file in laravel 11:

Step 1 – Create a Route File

Create a custom route file in routes folders. For example, i will create myroutes.php file in routes folder.

Step 2 – Set Up Custom Route File

The custom route file needs to be configured in the bootstrap/app.php file, which can be used. Like the following:

use Illuminate\Support\Facades\Route;
 
->withRouting(
    web: __DIR__.'/../routes/web.php',
    commands: __DIR__.'/../routes/console.php',
    health: '/up',
    then: function () {
        Route::middleware('myroutes')
            ->prefix('myroutes')
            ->name('myroutes.')
            ->group(base_path('routes/myroutes.php'));
    },
)

Step 3 – Add Routes

To add custom routes in custom route file name myroutes.php; like the following:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/test', function () {
    return "This is my first custom route in custom route file";
});

Step 4 – Test it in Browser

Now you can test your custom routes from routes file on the browser: like the following:

http://127.0.0.1:8000/myroutes/test

It’s easy to create and configure a custom routes file with routes using a 4-step guide.

Leave a Comment