Laravel Create Custom Route File

Create custom route file in laravel; Through this tutorial, i am going to show you how to create custom route file or multiple routes file in laravel apps.

How to Create Custom Route File in Laravel

Follow the below given steps to create custom route file in laravel apps; is as follows:

  • Step 1: Install Laravel App
  • Step 2: Create Custom Route File
  • Step 3: Add Route Files to ServiceProvider

Step 1: Install Laravel App

Run the following command on command prompt to install laravel fresh application into your system:

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

Step 2: Create Custom Route File

Go to your app/routes folder and create custom routes file. And will create custom routes file name students.php.

Then add routes in this file according to your requirements.

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

Step 3: Add Files to ServiceProvider

Go to app/Providers folder and find RouteServiceProvider.php . Then open it and register your custom routes file as follow:

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

To add your routes in student.php route file and use it as follow:

 http://localhost:8000/student/* 

Recommended Laravel Tutorials

Leave a Comment