How to Create Custom Route File in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

In this tutorial, i will show you laravel create custom route file example. you can understand a concept of laravel add custom route file example. step by step explain how to add custom route file in laravel. i explained simply about how to create custom route file in laravel. You just need to some step to done define custom route file in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

If you are working with large application in laravel and you have different types of users then you have to create custom route file for user type wise in your application. for example if you have user, manager and admin three types of user then you have different prefix like "user/*", "manager/*" and "admin/*" url will be. so if you create different route file then you can easily make it better way your routes.

In this example, i will let you know how you to create custom route file in laravel application. you can easily use it with laravel 5, laravel 6 and laravel 7. So let's follow bellow steps:

In this tutorial, I am going to share with you how to define and use subdomain routes better way in Laravel 5 application.

Step 1 : Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Custom Route File

Here, we will create following route file with bellow listed route. so let's create route file.

1) User Routes : Here you have to define routes in web.php in routes folder. in that file you have declare routes for user login.

2) Manager Routes : Here you have to create new file manager.php in routes folder. in that file you have declare routes for manager user.

3) Admin Routes : Here you have to create new file admin.php in routes folder. in that file you have declare routes for admin user.

So, let's proceed with routes define:

routes/web.php

<?php


/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/


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

dd('Welcome to simple user route file.');

});

routes/manager.php

<?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 manager user routes.');

});

routes/admin.php

<?php


/*

|--------------------------------------------------------------------------

| Admin Routes

|--------------------------------------------------------------------------

|

| Here is where you can register admin routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "admin" middleware group. Now create something great!

|

*/


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

dd('Welcome to admin user routes.');

});

Step 3: Add Files to ServiceProvider

In this step, we require to register our new two user file in RouteServiceProvider, that way we can create new file for each user wise like we create two user "admin" and "manager" then we create new file admin.php and manager.php in routes directory for define routing.

So, let's open RouteServiceProvider.php and put bellow code:

app/Providers/RouteServiceProvider.php

<?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->mapAdminRoutes();


$this->mapManagerRoutes();

}


/**

* 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 "admin" routes for the application.

*

* These routes are typically stateless.

*

* @return void

*/

protected function mapAdminRoutes()

{

Route::prefix('admin')

->namespace($this->namespace)

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

}


/**

* Define the "user" routes for the application.

*

* These routes are typically stateless.

*

* @return void

*/

protected function mapManagerRoutes()

{

Route::prefix('manager')

->namespace($this->namespace)

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

}


}

Ok, now we are ready to run simple example with our users. So, in this tutorial i explained i created following url as listed bellow:

1) http://localhost:8000/

2) http://localhost:8000/manager/*

3) http://localhost:8000/admin/*

I hope it can help you.

Shares