How to Create Custom Route File in Laravel


Share:
Published By: Admin, Published On: May 06, 2021, Category: Laravel

Hello Friends,

Now let's see example of how to create custom route file in laravel example. This is a short guide on laravel custom route file. Here you will learn create custom route file laravel. We will use create custom route file in laravel. Let's get started with create custom route file in laravel.

Here i will give you few step instruction to create custom route file in laravel.

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) Employee Routes : Here you have to create new file employee.php in routes folder. in that file you have declare routes for employee user.

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

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/employee.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 employee user routes.');
});
?>

routes/client.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 client 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 "client" and "employee" then we create new file client.php and employee.php in routes directory for define routing.

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->mapClientRoutes();


        $this->mapEmployeeRoutes();
    }

    
    /**
     * 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 "client" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapClientRoutes()
    {
        Route::prefix('client')
            ->namespace($this->namespace)
            ->group(base_path('routes/client.php'));
    }


    /**
     * Define the "user" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapEmployeeRoutes()
    {
        Route::prefix('employee')
            ->namespace($this->namespace)
            ->group(base_path('routes/employee.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/employee/*

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

It will help you....

Share: