Laravel

How to Create a Custom Route in Laravel?

Pinterest LinkedIn Tumblr

Step 1: Install the laravel project using the command line.

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

Step 2: Open project in code and use vs code terminal.

cd laravelCustomRoutes

Step 3: Create Custom Route File

Here, I will create following route file with multiple listed route.

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

2) Admin Routes: Here you have to create new file admin.php in routes folder. in that file, you have declared routes for the admin user.

3) Blog Routes: Here you have to create new file blog.php in routes folder. in that file, you have declared routes for the blog.

Add below code in routes.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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 () {
    return view('welcome');
});

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 routes.');
});

Routes/blog.php

<?php


/*
|--------------------------------------------------------------------------
| Blog Routes
|--------------------------------------------------------------------------
|
| Here is where you can register blog routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "blog" middleware group. Now create something great!
|
*/


Route::get('/', function () {
    dd('Welcome to blog routes.');
});

Step 5: Add Files to ServiceProvider

In this step, we require to register our new two user file in RouteServiceProvider, that way we can create a new file for each user wise like we create two “admin” and “blog” then we create new file admin.php and blog.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->mapBlogRoutes();
    }

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

Step 5: Run Server

php artisan serve
http://127.0.0.1:8000/blog
http://127.0.0.1:8000/admin

Write A Comment