How to Create Custom Route File in Laravel App

How to Create Custom Route File in Laravel App

In this custom route file example tutorial, you will learn how to create custom routes file and use them.

When you are working with the large applications in laravel and you have different types of users. At that time you need to create custom route file for each types of user into your laravel apps.

For example, when you develop school manage system in laravel. So you have students, teachers, management, and super admin, admin types of user into your laravel apps. So if you create a different route file for different types of user.

Here, you will learn step by step from scarch, how to create custom route file in laravel application and as well as how to use it.

Note that, You can use this example with laravel different version laravel 5, 6, 7.

Laravel Create Custom Route File Example

Follow the below steps and create custom route file and use it in laravel web apps:

Step 1: Install Laravel Fresh Application

First of all, open your command prompt and run the following command to install laravel fresh application into your system:

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

Step 2: Create Custom Route File

Default Routes : Laravel provides default routes file name web.php and api.php. In these files you can define your routes and use it.

But what about custom routes files?.

Now you will learn how to create custom route file and use it.

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

routes/student.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

In this step, you need to register your custom routes in Route Service Provider.

So, 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'));
    }



}

Now you can add your routes in student.php route file and use it as follow:

 http://localhost:8000/student/* 

Conclusion

In this tutorial, you have learned how to create custom route file and use it. As well as how to register custom route file in routes service provider.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

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