Site Admin
Site Admin Founder of MeaningArticles
1516 Views

Laravel Sub-domain routing file Example

Hello Dev.

in this article, i am going to share with you how to define and use subdomain routes better way in laravel 5, laravel 6, laravel 7 and laravel 8 application.

nowadays, we need to create sub-domain of our main domain. however if you are the use of core php or something, then you need to create code for brand new sub-domain and it always take time to develop again and again. however, In Laravel framework 5, they offer group routing that way we will define subdomain routes in same utility or project of laravel. we can easily manage sub-domain from our main project that way we don't require to create always new project and code and many others. but we can simply control through single laravel application.

So, in this article i'm give you quite simple example of use sub-domain routing of laravel. you need to just follow few step to create simple instance from scratch. you could easily create this example on your localhost too.

In this instance, we can create one major domain with two sub domain, as listed bellow:

1. example.ai
2. admin.example.ai
3. user.example.ai

right here, as listed above three domain. this instance for only local. So i created there domain. we are able to simply control it with the aid of single laravel application. So permit's continue.


Step 1: Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

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


Step 2: Add Sub-Domain in ServiceProvider

in this step, we require to register in our new two subdomain in RouteServiceProvider, that way we can create new file for every subdomin like we create two sub-domain "admin" and "user" then we create new file admin.php and user.php in routes directory for define routing.

So, let's open RouteServiceProvider.php and put bellow code:
app/Providers/RouteServiceProvider.phpcode

<?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->mapUserRoutes();
    }
    
    /**
     * 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::namespace($this->namespace)
             ->group(base_path('routes/admin.php'));
    }

    /**
     * Define the "user" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapUserRoutes()
    {
        Route::namespace($this->namespace)
             ->group(base_path('routes/user.php'));
    }

}


Step 3: Add Sub-Domain Routes

In last step, we want to add routes for our main domain and two sub-domain that way we can easily make example. So here first you have to just add new route in your web.php file for main domain and other two as listed bellow:

1. Admin Routes: Here you have to create new file admin.php in routes folder. in that file you have declare routes for admin sub domain.

2. User Routes: Here you have to create new file user.php in routes folder. in that file you have declare routes for user sub domain.

So, let's proceed with routes define:
routes/web.php

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| here is wherein you may register web routes for your application. these
| routes are loaded with the aid of the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something tremendous!
|
*/
Route::get('/', function () {
    dd('Welcome to main domain.');
});

routes/admin.php

<?php
/*
|--------------------------------------------------------------------------
| Admin Routes
|--------------------------------------------------------------------------
|
| here is wherein you may register web routes for your application. these
| routes are loaded with the aid of the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something tremendous!
|
*/
Route::get('/', function () {
    dd('Welcome to admin subdomain.');
});

routes/user.php

<?php
/*
|--------------------------------------------------------------------------
| User Routes
|--------------------------------------------------------------------------
|
| here is wherein you may register web routes for your application. these
| routes are loaded with the aid of the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something tremendous!
|
*/
Route::get('/', function () {
    dd('Welcome to user subdomain.');
});

ok, now we're prepared to run simple example with our sub-domain. So, on this tutorial i defined, i created two subdomain as listed above. So for locally we should create three domain using virtualhost like as bellow:

1. example.ai
2. admin.example.ai
3. user.example.ai

You have to simple run in your browser. So basically you have to create virtual host.

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.