Thursday 30 March 2017

Laravel 5.4 Sub domain routing method with in application

Today I am going to share with you how to define and use sub domain routes better way in Laravel 5 application.

We almost need to create sub-domain of our main domain. But if you are using core PHP or Codeigniter, then you have to create code for new sub-domain and it always take time to develop again and again. However, In Laravel framework 5, they provide group routing that way we can define sub domain routes in same application or project of laravel. We can simply manage sub-domain from our main project that way we don't require to create always new project and code etc. But we can simply manage by single laravel application. so that we can reduce the file size in our server.

In this post i am going give you very simple example of use sub-domain routing of laravel. you have to just follow few step to create simple example from scratch. You can simply create this example in your localhost too.

In this example, we will create one main domain with two sub domain, as listed bellow:

1) laravelpractice.com
2) admin.laravelpractice.com
3) user.laravelpractice.com
Here, as listed above three domain. This example for only local. So i created there three domain. we will simply manage it by single laravel application. So let's proceed.

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 our new two subdomain in RouteServiceProvider, that way we can create new file for each 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.phpph
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 need to add routes for our main domain and two sub-domain that way we can simply 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

Route::get('/', function () {
dd('Welcome to main domain.');
});

routes/admin.php

Route::get('/', function () {
dd('Welcome to admin subdomain.');
});

routes/user.php

Route::get('/', function () {
dd('Welcome to user subdomain.');
});

Now we are ready to run simple example with our sub-domain. So, in this tutorial i explained i created two subdomain as listed above. So for locally we have to create three domain using virtualhost like as bellow:

1) laravelpractice.com
2) admin.laravelpractice.com
3) user.laravelpractice.com

You have to simple run in your browser.

No comments:

Post a Comment

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in Laravel

  WampServer: Download this file:  http://curl.haxx.se/ca/cacert.pem Place this file in the  C:\wamp64\bin\php\php7.1.9  folder Open  php.in...

Popular Articles