Create Custom Route File With New Service Provider Laravel 8 9

  • 30-08-2022
  • 1138
  • Laravel 9
  • Haresh Chauhan

How to make a custom route file with help of a new route service provider, In This article we will create a custom route file with the new service provider. Sometimes developer uses the same route service provider for different route files. But in this post, we will create a new service provider for the new route file.

For creating a custom new route file, we also need to register that file in the route service provider. In that case, we also create a new service provider for the new route file.

Creating a new custom route file also with the new route service provider, taking an example for the front route separate from the admin route.

Step 1. Create Service Provider

Use the below provided contain in your terminal, The command will create a FrontRoutesServiceProvider.php file inside your provider's folder in your application.

The GET method is not supported for this route. Supported methods: PATCH

php artisan make:provider FrontRoutesServiceProvider

Add your controller namespace. register your parent boot method. register your front.php route file in the mapFrontRoutes() method and then call this method in the map() method as per the below example given. Therefore first you need to create a front.php file inside the routes folder. You can also give a name as per your requirement.

app/providers/FrontRoutesServiceProvider.php
<?php

namespace App\Providers;

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

class FrontRoutesServiceProvider extends ServiceProvider
{
    // APPLY ONLY THE CONTROLLER AVAILABLE IN FRONT FOLDER INSIDE THE CONTROLLERS
    
    protected $namespace = 'App\Http\Controllers\Front'; // NAMESPACE FOR PREFIX FRONT CONTROLLER PATCH
    

    /**
        * Register services.
        *
        * @return void
        */
    public function register()
    {
        //
    }

    /**
        * Bootstrap services.
        *
        * @return void
        */
    public function boot()
    {
        parent::boot();
    }

    public function map()
    {
        $this->mapFrontRoutes();
    }

    protected function mapFrontRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/front.php')); // CREATE front.php FILE IN ROUTES FOLDER
    }
}

Register your FrontRoutesServiceProvider in the app.php, as per the below example given.

config/app.php
'providers' => [
    .......
    App\Providers\FrontRoutesServiceProvider::class,
],

Step 2. Create New Route File

Create a UserController by giving the below-provided command in your terminal. Here you just need to give that namespace according to the given in-route service provided given.

php artisan make:controller Front/UserController

In your front.php file first import use Illuminate\Support\Facades\Route, and also import the controller.

routes/front.php
<?php
    
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\Front\UserController;

Route::get('front-users',[UserController::class,'frontUsers']);

This is our user controller, the controller call from the front.php route file.

app/Http/Controllers/Front/UserController.php
<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function frontUsers()
    {
        dd('Your New Routes File Ready To use');
    }
}

Step 3. Start Deveopment Server

Start your development server, and use the following command in your terminal.

php artisan serve

Run your URL in your browser as per your given.

http://127.0.0.1:8000/front-users

Route Resource Controller Laravel


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :