PHP Laravel 5 – Sub-domain routing file Example

PHP Laravel 5 – Sub-domain routing file Example

In this post we will give you information about PHP Laravel 5 – Sub-domain routing file Example. Hear we will give you detail about PHP Laravel 5 – Sub-domain routing file ExampleAnd how to use it also give you demo for it if it is necessary.

In this tutorial, I am going to share with you how to define and use subdomain routes better way in Laravel 5 application.

In Today, we almost need to create sub-domain of our main domain. But if you are using core PHP or something, 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 subdomain 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, 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) blogl54.hd

2) admin.blogl54.hd

3) user.blogl54.hd

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.php

<?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();


$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

<?php


/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/


Route::get('/', function () {

dd('Welcome to main domain.');

});

routes/admin.php

<?php


/*

|--------------------------------------------------------------------------

| Admin Routes

|--------------------------------------------------------------------------

|

| Here is where you can register admin routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "admin" middleware group. Now create something great!

|

*/


Route::get('/', function () {

dd('Welcome to admin subdomain.');

});

routes/user.php

Also see:How to execute artisan commands from route or controller in Laravel ?

<?php


/*

|--------------------------------------------------------------------------

| User Routes

|--------------------------------------------------------------------------

|

| Here is where you can register user routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "user" middleware group. Now create something great!

|

*/


Route::get('/', function () {

dd('Welcome to user subdomain.');

});

Ok, 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) blogl54.hd

2) admin.blogl54.hd

3) user.blogl54.hd

You have to simple run in your browser. So basically you have to create virtual host. If you are using Linux then you can quick create virtual host using this link : Create Virtual Host in Linux.

I hope it can help you.

Hope this code and post will helped you for implement PHP Laravel 5 – Sub-domain routing file Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

39  +    =  47

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US