How to create custom route file in laravel
You can create your own customize laravel route file in your project. So what is laravel custom route file means? Sometimes this is very essentials to organize your projects. Suppose you have three types of users. Seems like admin, merchant, customer and each type of users have a different type of features in your application. For that kind of scenario, you can create different route files like admin_route, merchant_route, customer_route. This is a very easy way to manage laravel custom route files in your projects.
First of all, we need to realize the existing code. See there is a method named map(). Add new two method named mapMerchantRoutes() for & mapClientRoutes()
File Name : app/providers/RouteServiceProvider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
** * Define the routes for the application. * * @return void */ public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); $this->mapAdminRoutes(); $this->mapMerchantRoutes(); $this->mapCustomerRoutes(); // } |
in this method, two another method is called named mapApiRoutes() & mapWebRoutes(). If we want to see those two methods then we will that this method is mapping two individual route files named web.php & api.php in the route folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 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')); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * 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')); } |
Therefore we are creating new three mapping method in app/providers/RouteServiceProvider.php file similar like mapWebRoutes() or method mapApiRoutes()
Most importantly our new thee method name is mapAdminRoutes(), mapMerchantRoutes() & mapCustomerRoutes().
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Define the "admin" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapAdminRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/admin.php')); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Define the "merchant" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapMerchantRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/merchant.php')); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Define the "customer" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapCustomerRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/customer.php')); } |
Here we are seeing that three methods with mapping three route file name admin.php, merchant.php & customer.php. Now if we have to separate all route with different files like
FIrstly, admin routes should be in route/admin.phpÂ
Secondly, merchants and Finally customer routes should be gradually in route/merchant.php and  route/customer.phpÂ
The final RouteServiceProvider.php file looks like this.
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
<?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() { // Route::pattern('id', '[0-9]+'); parent::boot(); } /** * Define the routes for the application. * * @return void */ public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); $this->mapAdminRoutes(); $this->mapMerchantRoutes(); $this->mapCustomerRoutes(); // } /** * 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 "admin" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapAdminRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/admin.php')); } /** * Define the "merchant" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapMerchantRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/merchant.php')); } /** * Define the "customer" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @return void */ protected function mapCustomerRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/customer.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')); } } |
After separating all routes it should be run your projects
What’s up, after reading this awesome paragraph i am too cheerful to share my experience here with colleagues.
Thanks a lot.
Enjoy your coding….
Hi, for any new route add prefix for example:protected function mapAdminRoutes(){Route::middleware(‘web’)#important for CSRF protection->prefix(‘admin’)->namespace($this->namespace)->group(base_path(‘routes/admin.php’));}