Reputation: 16283
I would like to be able to use different routes within my application for different domains. I would like to act differently depending on whether the domain is
mysite.com/something
subdomain.mysite.com/something
anotherdomain.com
I've approach the problem like this:
// Match my own domain
Route::group(['domain' => 'mysite.com'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
// Match a subdomain of my domain
Route::group(['domain' => '{subdomain}.mysite.com'], function()
{
Route::any('/', function($subdomain)
{
return 'Subdomain ' . $subdomain;
});
});
// Match any other domains
Route::group(['domain' => '{domain}'], function()
{
Route::any('/', function()
{
return 'Full domain ';// . $domain;
});
});
The first two groups work perfectly. Visiting mysite.com
displays My own domain
and visiting subdomain.mysite.com
displayed Subdomain subdomain
as expected. However, when I visit with anotherdomain.com
(I have this set up as an alias in my vhost file as well as pointing it to the loopback IP in my hosts file), I get an NotFoundHttpException
:
/var/www/portfolio/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php
Code:
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0)
{
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException;
}
Is there a way I can match any domain that is not my domain or a subdomain of my domain in this way? I need to be able to access the domain to do something with it afterwards too, just like I did with $subdomain
.
Thanks, Jonathon
Upvotes: 4
Views: 8049
Reputation: 189
This is a late response, but you can do dynamic full domain routing without resorting to ugly extension hacks. By default, the Laravel domain filter applies a regexp that prevents you from using period characters in the value passed to the filter. Hence why it works when you break it out into segments, but not if you try to pass the full domain as a single parameter.
The easiest solution is to modify the pattern applied to the domain filter in the boot
method of your app/Providers/RouteServiceProvider.php
file as follows:
public function boot(Router $router)
{
$router->pattern('domain', '[a-z0-9.]+');
parent::boot($router);
}
This will allow you to send full domains through the filter.
Upvotes: 0
Reputation: 136
Was searching for this too...
Probably not the most beautiful solution, but it works
// Match my own domain
Route::group(['domain' => 'mysite.com'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
// Match a subdomain of my domain
Route::group(['domain' => '{subdomain}.mysite.com'], function()
{
Route::any('/', function($subdomain)
{
return 'Subdomain ' . $subdomain;
});
});
// Match any other domains
Route::group(['domain' => '{domain}.{tld}'], function(){
Route::any('/', function($domain, $tld){
return 'Domain: ' . $domain . '.' . $tld;
});
});
Route::group(['domain' => '{subdomain}.{domain}.{tld}'], function(){
Route::any('/', function($sub, $domain, $tld){
return 'subdomain: ' . $sub . '.' . $domain . '.' . $tld;
});
});
By the way, if you like to test it, add some fake domains to your HOSTS file, and point them to 127.0.0.1 :-)
Upvotes: 10
Reputation: 3543
Looks like the question is old. However, you could use the following:
Just remove the group part from the code as this will apply to any other domain
Route::any('/', function()
{
return 'Full domain ';// . $domain;
});
Upvotes: -5
Reputation: 1082
I think that isn't possible because you have declared your domain in config/app.php.
You could try creating separate environment for second domain.
You can view how to use second domain in Laravel in this tutorial.
Upvotes: 0