Can I group multiple domains in a routing group in Laravel?

34,618

Solution 1

Laravel does not seem to support this.

I'm not sure why I didn't think of this sooner, but I guess one solution would be to just declare the routes in a separate function as pass it to both route groups.

Route::group(array('domain' => 'admin.example.com'), function()
{
    ...
});

$appRoutes = function() {
    Route::get('/',function(){
        ...
    }); 
};

Route::group(array('domain' => 'app.example.com'), $appRoutes);
Route::group(array('domain' => 'dev.app.example.com'), $appRoutes);

I'm not sure if there is any significant performance impact to this solution.

Solution 2

Laravel 5.1

 

Route::pattern('subdomain', '(dev.app|app)');
Route::group(['domain' => '{subdomain}.example.com'], function () {
  ...
});

 

Route::pattern('subdomain', '(dev.app|app)');
Route::pattern('domain', '(example.com|example.dev)');
Route::group(['domain' => '{subdomain}.{domain}'], function () {
  ...
});

Solution 3

You can pass on the domain name as well:

Route::pattern('domain', '(domain1.develop|domain2.develop|domain.com)');
Route::group(['domain' => '{domain}'], function() {
    Route::get('/', function($domain) {
        return 'This is the page for ' . $domain . '!';
    });
});

Just in case you need to know with which domain name the controller is called. Tested it with Laravel 5.6.

Solution 4

Interested in this also! I'm trying to register a local development + production subdomain route, for the one controller action.

i.e.

# Local Dev
Route::group(array('domain' => "{subdomain}.app.dev"), function() {
    Route::get('/{id}', 'SomeController@getShow');
});

# Production Server
Route::group(array('domain' => "{subdomain}.app.com"), function() {
    Route::get('/{id}', 'SomeController@getShow');
});

I tried:

# Failed
Route::group(array('domain' => "{account}.app.{top_level_domain}"), function() {
    Route::get('/{id}', 'SomeController@getShow');
});

But it failed.

Not a huge issue, as DesignerGuy mentioned I can just pass in a function to both routes - but it would just be more elegant if they could be grouped :)

Solution 5

check in laravel docs, if you main domain is myapp, in production is myapp.com and in local environment is myapp.dev try using a *

Route::group(array('domain' => '{subdomain}.myapp.*'), 
function()
{
    ...
});
Share:
34,618
Andy Fleming
Author by

Andy Fleming

Updated on May 01, 2020

Comments

  • Andy Fleming
    Andy Fleming about 4 years

    Let's say I have the following:

    Route::group(array('domain' => array('admin.example.com')), function()
    {
        ...
    });
    
    Route::group(array('domain' => array('app.example.com')), function()
    {
        ...
    });
    
    Route::group(array('domain' => array('dev.app.example.com')), function()
    {
        ...
    });
    

    Is there any way to have multiple domains share a routing group? Something like:

    Route::group(array('domain' => array('dev.app.example.com','app.example.com')), function()
    {
        ...
    });
    
  • Adam Lenda
    Adam Lenda about 10 years
    I am attempting to solve a similar problem. Namely, to have a "ui.domain.com" and an "api.domain.com" in production, yet allow my development team to have "ui.domain.local" and "api.domain.local" for their local development environments. Support for a wild card after the sub-domain would do the trick. The basic problem is that I am aiming to solve is to resolve conflicting routes. For example ui.domain.com/messages should return HTML and api.domain.com/messages should return JSON. So two groups with 'domain' => 'api.(:any)' and 'domain' => 'ui.(:any)' would be ideal
  • Ed B
    Ed B about 8 years
    This is a great tip. Be aware, though, that the domain parameter will be passed as the first parameter of any child routes: Route::get('users/{id}', 'UsersController@show'); // id = "example.com"; To avoid this you can always use environment variables instead: $domain = env('BASE_DOMAIN', 'example.com'); Route::group(['domain' => 'subdomain.'.$domain], function() { ... });
  • Ivan
    Ivan over 7 years
    this not work <code> Route::group(array('domain' => '{maindomain}'), function() { ... })->where('maindomain', '.+\.example\.com$'); </code>
  • marknt15
    marknt15 about 7 years
    Thanks for this. I think there's no performance impact to this because you're just storing it in a array variable and just padding it to the 2 declaration. Cheers!
  • Fredrik
    Fredrik about 5 years
    Would this really work with php artisan route:cache? laravel.com/docs/5.8/controllers#route-caching "Closure based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes."
  • Andy Fleming
    Andy Fleming about 5 years
    I'm not sure @Fredrik, but that quote may be speaking to the actual route implementation and not the construction of the routes. In my example above, if Route::get referenced a controller method, it might be cacheable.
  • sgtcoder
    sgtcoder over 2 years
    I added a comment to this link you provided with an improvement for named routes: github.com/laravel/framework/issues/4017#issuecomment-897936‌​526