In this laravel custom route file example tutorial, you will learn how to create custom routes file and use them in laravel 10|9|8 apps.
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.
Here, you will learn step by step from scarch, how to create custom route file in laravel 10|9|8 application and as well as how to use it.
Laravel 10|9|8 Create Custom Route File Example Tutorial
Steps to create custom route file and use it in laravel 10|9|8 web apps; is as follows:
Step 1: Install Laravel Fresh Application
First of all, open your command prompt and run the following command to install laravel fresh application into your system:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Custom Route File
Default Routes : Laravel provides default routes file name web.php and api.php. In these files you can define your routes and use it.
But what about custom routes files?.
Now you will learn how to create custom route file and use it.
Go to your appName/routes folder and create custom routes file. And will create custom routes file name students.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 3: Add Files to ServiceProvider
In this step, you need to register your custom routes in Route Service Provider.
So, Go to app/Providers folder and find RouteServiceProvider.php . Then open it and register your custom routes file as follow:
<?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')); } }
Now you can add your routes in student.php route file and use it as follow:
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.