Organizing the routes in Laravel

By default Laravel provides us 4 routes files. These files are pretty much enough for managing routing system for an application. We can divide the routes in other files as well for managing the routings. First let’s see what the 4 routes are meant to be used for.

api.php This is where our api routes belongs.
channels.php This is where our broadcasting channel exists.
console.php This is where our artisan commands belongs.
web.php This files contains our routes for the application.

How do we organize our routes for better maintainability ?

Let’s be frank, of course we can manage the whole routes on our web.php which seems to be the one way to manage all the routes. Using middleware , group, prefix and other routes attributes we can easily manage the routes for an application. But no kidding the more the application get huge the more the routes will get bigger and unmanageable.

Today we are going to split the web.php in admin.php and frontend.php route files for good. First let’s create admin.php and frontend.php in routes directory.

Secondly, let’s get to the file and register the new routes we’ve just created.

AppProvidersRouteServiceProvider.php

How do we register the routes ?

Let’s add two functions for mapping the routes.

    /* Admin routes */
    protected function mapAdminRoutes()
    {
        Route::prefix('admin')
            ->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/admin.php'));
    }
    /* Frontend routes */
    protected function mapFrontendRoutes()
    {
        Route::prefix('frontend')
            ->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/frontend.php'));
    }

Let’s define the both routes to the map() function in the routeServiceProvider class.

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes();
        // For admin
        $this->mapAdminRoutes();
        // For Frontend
        $this->mapFrontendRoutes();
    }

The final result will be like this.

<?php
namespace AppProviders;
use IlluminateSupportFacadesRoute;
use IlluminateFoundationSupportProvidersRouteServiceProvider 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 = 'AppHttpControllers';
    /**
     * 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();
        // For admin
        $this->mapAdminRoutes();
        // For Frontend
        $this->mapFrontendRoutes();
    }
    /**
     * 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'));
    }
    /* Admin routes */
    protected function mapAdminRoutes()
    {
        Route::prefix('admin')
            ->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/admin.php'));
    }
    /* Frontend routes */
    protected function mapFrontendRoutes()
    {
        Route::prefix('frontend')
            ->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/frontend.php'));
    }
}

Lastly let’s test the routes files. Create FrontendController and AdminController files with index function where we add the below code on both controller.

public function index()
{
    dd('Hello From Admin/Frontend');
}

Now add the route to the routes file.

<?php
// frontend.php
Route::get('yo', 'FrontendController@index');
<?php
// admin.php
Route::get('yo', 'AdminController@index');

We have prefixed the admin route with ‘admin’ and frontend route with ‘frontend’. Let’s look at the list of routes.

Hope this tutorials help you organize your routes better and if you do have better option please do share. Happy coding 🙂

Leave a Reply

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

Pin It on Pinterest