Search code examples
phplaravel-5routessubdomain

Laravel 5.2 subdomain routing by using the Router function


I have an application built using Laravel 5.2. The application has 3-4 different types of entirely different use case, (mini applications, if you may). So, I am hosting the main site and the routes for admins on the main domain. For each of the mini applications, I have created one subdomain and all the mini application routes have their own subdomains. The way I have mapped subdomains are:

$sub = str_replace('http://', '', strstr(Request::fullUrl(), '.', true));

if ( $sub == env('APP_SUB1') ) { 

 //Subdomain1 routes

} else if ( $sub == env('APP_SUB2') ) { 

//Subdomain2 routes

}

Now, I want to be able to use the Laravel subdomain in-built subdomain routes, and I tried:

Route::group(['domain' => env('APP_DOMAIN')], function () {
    Route::get('/', function () {
        echo 'Main Site';
        die;
    });
});

Route::group(['domain' => '{sub1}.'.env('APP_DOMAIN')], function () {
    Route::get('/', function ($sub1) {
        echo 'Sub1 Site';
        die;
    });
});

Route::group(['domain' => '{sub2}.'.env('APP_DOMAIN')], function () {
    Route::get('/', function ($sub2) {
        echo 'Sub2';
        die;
    });
});

UPDATE

A little more information about the behavior:

  1. When a user visits www.mydomain.com/login, the login page for the admins should be shown.
  2. When a user visits sub1.mydomain.com/login, the login page for the users of sub1 application should be shown.
  3. When a user visits sub2.mydomain.com/login, the login page for the users of sub2 application should be shown, and so on...

My earlier method by parsing the full request url and then routing the users accordingly, works. What I want is a more robust and elegant way (so tried using laravel domain routing). But with the example code that I showed above, with the subdomains, I always see 'Sub1 Site', irrespective of the subdomain passed.


Solution

  • I think there's a bit of a misunderstanding on how it's meant to work. If you pass the subdomain in {} then you're basically saying it's a variable subdomain, meaning '{sub1}.'.env('APP_DOMAIN') and '{sub2}.'.env('APP_DOMAIN') are essentially the same thing but with a different variable name for the subdomain:

    Example:

    Route::group(['domain' => '{sub1}.'.env('APP_DOMAIN')], function () {
        Route::get('/', function ($sub1) {
           echo $sub1." Site";
           //Prints sub1 Site when visiting sub1 and sub2 site when visiting sub2
           die;
       });
    }]); 
    

    What (I'm assuming) you want is:

    Route::group(['domain' => 'sub1.'.env('APP_DOMAIN')], function () {
        Route::get('/', function () {
           echo "Sub1 Site";
           die;
       });
    }]); 
    Route::group(['domain' => 'sub2.'.env('APP_DOMAIN')], function () {
        Route::get('/', function () {
           echo "Sub2 Site";
           die;
       });
    }]); 
    

    Notice the removal of {} this means the first group will match the literal sub1. instead of *.<rest of domain>