Reputation: 671
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:
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.
Upvotes: 0
Views: 619
Reputation: 40683
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>
Upvotes: 2
Reputation: 37
I tried without passing and in that case, all URLs printed only "sub1"
Of course, because the first route group catches all requests. You can completely remove the second route group, and add logic (which you shouldn't do in routes config) to handle subdomains.
{sub1} could be anything really, so when you enter, for example, " anythingreally.mydomain.com/login " it will still get caught, and pass subdomain to variable $sub1.
Upvotes: 3