To create custom route file in laravel 11 / 10; Simply navigate to the routes directory and create your custom route file in it, after that, add the custom route file in Providers/RouteServiceProvider.php
file.
When you are working with the large applications in laravel and you have different types of users. At that time you need to create custom route file for each types of user into your laravel apps. For example, when you develop school manage system in laravel. So you have students, teachers, management, and super admin, admin types of user into your laravel apps. So if you create a different route file for different types of user.
How to Create Custom Route File in Laravel 11/10
Steps to create custom route file and use it in laravel web apps:
Step 1: Create Custom Route File
Navigate to your root folder and create a custom route file that you can name anything you want. Here we will create custom route file name student.php.
routes/student.php
Then add routes in this file according to your requirements.
<?php /* |-------------------------------------------------------------------------- | User Routes |-------------------------------------------------------------------------- | | Here is where you can register user routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "user" middleware group. Now create something great! | */ Route::get('/', function () { dd('Welcome to student routes.'); });
Step 2: Add Route Files to ServiceProvider
Navigate to app/Providers folder and open RouteServiceProvider.php. And add or register your custom routes file in it; like following:
<?php namespace App\Providers; use Illuminate\Support\Facades\Route; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; class RouteServiceProvider extends ServiceProvider { /** * This namespace is applied to your controller routes. * * In addition, it is set as the URL generator's root namespace. * * @var string */ protected $namespace = 'App\Http\Controllers'; /** * Define your route model bindings, pattern filters, etc. * * @return void */ public function boot() { parent::boot(); } /** * Define the routes for the application. * * @return void */ public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); $this->mapStudentRoutes(); } /** * Define the "web" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); } /** * Define the "api" routes for the application. * * These routes are typically stateless. * * @return void */ protected function mapApiRoutes() { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); } /** * Define the "student" routes for the application. * * These routes are typically stateless. * * @return void */ protected function mapStudentRoutes() { Route::prefix('admin') ->namespace($this->namespace) ->group(base_path('routes/student.php')); } }
Step 3: Use Custom Routes
Now you can add your routes in student.php route file and use it as follows:
http://localhost:8000/student/*
Conclusion
In this tutorial, you have learned how to create custom route file and use it. As well as how to register custom route file in routes service provider.