Loading image

Blogs / Programming

How to Create Custom Route File in Laravel 11

How to Create Custom Route File in Laravel 11

  • showkat ali
  • 0 Comments
  • 5389 View

Step-by-Step Guide : How to Create Custom Route File in Laravel 11

In Laravel 11, the process of creating a custom routes file and configuring it in the application has changed. But it has become easier than even older versions of Laravel.

In this easy and short guide, we will show you how to create a custom route file and use it in laravel 11 application, with example.

 

Step 1: Create a Custom Route File

Go to routes folder and make custom route file named teacher.php.

After that, add the following test route to admin.php file, something like this:

<?php

use Illuminate\Support\Facades\Route;

//==== admin.php ======//

Route::get('/', function () {

    return "welcome to admin route";

});

 

 

Step 2: Add Route Files to App.php

Open app.php file from bootstrap folder and add or register your custom routes file in it; something like this:

<?php

 

use Illuminate\Foundation\Application;

 

return Application::configure(basePath: dirname(__DIR__))

    ->withRouting(

        web: __DIR__.'/../routes/web.php',

        commands: __DIR__.'/../routes/console.php',

        health: '/up',

    )->create();

However, sometimes you may want to define an entirely new file to contain a subset of your application's routes. To accomplish this, you may provide a then closure to the routing method. Within this closure, you may register any additional routes that are necessary for your application:

 

use Illuminate\Support\Facades\Route;

 

->withRouting(

    web: __DIR__.'/../routes/web.php',

    commands: __DIR__.'/../routes/console.php',

    health: '/up',

    then: function () {

        Route::middleware('web')

            ->prefix('admin')

            ->name('admin.')

            ->group(base_path('routes/admin.php'));

    },

)

Or, you may even take complete control over route registration by providing a closure to the withRouting method. When this argument is passed, no HTTP routes will be registered by the framework and you are responsible for manually registering all routes.

use Illuminate\Support\Facades\Route;

 

->withRouting(

    commands: __DIR__.'/../routes/console.php',

    using: function () {

        Route::middleware('api')

            ->prefix('api')

            ->group(base_path('routes/api.php'));

 

        Route::middleware('web')

            ->group(base_path('routes/web.php'));

    },

)

Step 3: Use Custom Routes

Now you can define your routes in custom admin.php route file and use it as follows:

http://localhost:8000/admin/* 

 

Conclusion

This is simple and short guide on how to create and use custom route file in Laravel 11 application.

 

If you know or want more information about custom routing in the Laravel 11 version, you can check out https://laravel.com/docs/11.x/routing#routing-customization.

 

  • Programming
showkat ali Author

showkat ali

Greetings, I'm a passionate full-stack developer and entrepreneur. I specialize in PHP, Laravel, React.js, Node.js, JavaScript, and Python. I own interviewsolutionshub.com, where I share tech tutorials, tips, and interview questions. I'm a firm believer in hard work and consistency. Welcome to interviewsolutionshub.com, your source for tech insights and career guidance.

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

How to Handle Large Databases in Laravel: Best Practices for Managing Large Tables and Migrations

How to Handle Large Databases in Laravel: Best Practices for Managing Large Tables and Migrations

showkat ali
/
Programming

Read More
Laravel Eloquent Tips — Using Accessors Effectively

Laravel Eloquent Tips — Using Accessors Effectively

showkat ali
/
Programming

Read More
The Power of Feedback: How Regular Check-Ins Can Transform Employee Performance and Engagement

The Power of Feedback: How Regular Check-Ins Can Transform Employee Performance and Engagement

rimsha akbar
/
Human Resource

Read More
Laravel 12: Release Date, Features, and Everything You Need to Know

Laravel 12: Release Date, Features, and Everything You Need to Know

showkat ali
/
Programming

Read More
How to use DataTables in Laravel 11

How to use DataTables in Laravel 11

showkat ali
/
Programming

Read More
How to Deploy a Next.js Application on AWS EC2 Using GitHub Actions

How to Deploy a Next.js Application on AWS EC2 Using GitHub Actions

showkat ali
/
Programming

Read More