Create a Custom route in Laravel 6

In this blog, I will teach you how to create a custom route in laravel.

Generally, Laravel provides the most popular two route system web.php and api.php but another scenario you want to create a route only for admin panel then you should create a custom route.

So, Let’s start our tutorial

Step 1: Create a new file in routes.

Create a new file name is admin.php


Step 2: Changes in RouteServiceProvider

Open file app\Providers\RouteServiceProvider.php and put the below code:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    protected $namespace = 'App\Http\Controllers';
    
    public function boot(){
        parent::boot();
    }
    public function map(){
        $this->mapApiRoutes();
        $this->mapWebRoutes();
        $this->mapAdminRoutes();
    }
    protected function mapWebRoutes(){
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }
    protected function mapApiRoutes(){
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }
    protected function mapAdminRoutes(){
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/admin.php'));
    }
}

Now you can put the code in your admin.php file

<?php

Route::get('/admin/login','Auth\AdminAuthController@getLogin')->name('admin.login');

I guess it will help you!

Happy Coding!

About Dhaval Shah

My name is Dhaval Shah. I'm a Laravel developer. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress, and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!