In this tutorial you will learn about the How to Create Custom Route File in Laravel App and its application with practical example.
How to Create Custom Route File in Laravel App
In this How to Create Custom Route File in Laravel App tutorial I’ll show you how to create custom route file in laravel. In this tutorial you will learn to create custom route file in your laravel project. In this step by step tutorial I’ll guide you through the process of creating custom route file in laravel.
Step 1: Install Laravel Fresh Application
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Create Custom Route File
In this step we will be creating custom route file. Go to appName/routes folder and create custom routes file. And will create custom routes file name students.php.
routes/student.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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. Go to app/Providers folder and find RouteServiceProvider.php . Open it and register your custom routes file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
<?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:
1 |
http://localhost:8000/student/* |