Web Design & Development

Laravel Subdomain Routing

When developing modern web applications, it very common that different modules work in synergy or remain standalone. For example nowadays clients need to integrate admin panel with website(s) to manage their web operations with ease.

In this blog, I will demonstrate about Laravel subdomain routing to help you rout your requests correctly.

Prerequisites

To move forward, I prefer you should have a Laravel application installed on a web server. For now, I am using the following setup:

  1. Laravel 5.5
  2. PHP 7.1
  3. MySQL

To swiftly use application, I have deployed Laravel server on Cloudways managed hosting platform. It caters excellent features and advanced tools for optimized web services. You could also use Cloudways by signing up a free account on the platform.

Laravel Subdomain Routing

You can use route groups to handle subdomain routing. Sub-domains may be assigned route parameters just like route URLs, allowing you to capture an area or portion of the sub-domain for usage in your application route or controller. You must specify the sub-domain by calling the domain method before defining the group. For this purpose, write the following code.

Route::domain(‘{employee}.myapp.com’)->group(function () {

    Route::get(‘user/{id}’, function ($employee, $id) {

        //

    });

});

Setup Domain and Subdomain on Cloudways

For setting domain and subdomain on Cloudways, just go to the Application Management tab, then go to domain management for setting up domain and add subdomain.

Dynamically Generated Subdomains

It’s not a good solution, that I have fixed the subdomain in the route file since I use a different domain in local and production environment. I have set up the current domain with the APP_URL.env variable. I can use it to generate the proper subdomain depending on the environment, without hardcoding anything.

APP_URL=xyz.test

$domain = ‘{user}.’ . parse_url(config(‘app.url’), PHP_URL_HOST);

Route::domain($domain)->group(…);

Now I can change the APP_URL to anything because i parse it down to the host level, then I can append the fix subdomain or the wildcard parameter.

Service Provider Configuration

Here, you will have to register our new two subdomains in RouteServiceProvider. Now, you can create new file for each sub-domain, i.e. “admin” and “user”. After this, you have to create two new files, namely,  admin.php and user.php in the route directory for define routing.

Let’s open RouteServiceProvider.php and write the code as shown below:

<pre>

app/Providers/RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;

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

class RouteServiceProvider extends ServiceProvider

{

        protected $namespace = ‘App\Http\Controllers’;

    public function boot()

    {

        parent::boot();

    }

    public function map()

    {

        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapAdminRoutes();

        $this->mapUserRoutes();

    }

    protected function mapWebRoutes()

    {

        Route::middleware(‘web’)

             ->namespace($this->namespace)

             ->group(base_path(‘routes/web.php’));

    }

    protected function mapApiRoutes()

    {

        Route::prefix(‘api’)

             ->middleware(‘api’)

             ->namespace($this->namespace)

             ->group(base_path(‘routes/api.php’));

    }

      protected function mapAdminRoutes()

    {

        Route::namespace($this->namespace)

             ->group(base_path(‘routes/admin.php’));

    }

      protected function mapUserRoutes()

    {

        Route::namespace($this->namespace)

             ->group(base_path(‘routes/user.php’));

    }

}

</pre>

Add Sub-Domain Routes

Finally, you will have to add routes for our main domain and two sub-domains. You must first add new route in your web.php file for main domain and other two domains as shown below:

 1) Admin Routes: Here you have to create a new file admin.php in route folder. In that file you must declare route for admin sub-domain.

2) User Routes: Here you have to create a new file user.php in route folder. In that file you must declare routes for user sub-domain.

<pre>

routes/web.php

<?php

Route::get(‘/’, function () {

            dd(‘Welcome to main domain.’);

});

routes/admin.php

<?php

Route::get(‘/’, function () {

            dd(‘Welcome to admin subdomain.’);

});

routes/user.php

</pre>

Route::get(‘/’, function () {

            dd(‘Welcome to user subdomain.’);

});

</pre>

Final Words

When building a website or Laravel application there are variety of reasons that you need subdomain routing, In laravel is quite easy to set up subdomain routing but there are some code level changes you need to integrate within. In this article, I have elaborated about how you can configure subdomain routing in your Laravel applications to setup your website with admin panel easily.

Related posts

Top Reasons For iPhone App Development For Your Business

EmilyMario

3 Methods To Remove Author Name From WordPress Posts

Amit Gupta

Leave a Comment