Domanda

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()
{
    ...
});
È stato utile?

Soluzione

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.

Altri suggerimenti

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 () {
  ...
});

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.

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 :)

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()
{
    ...
});

according to laravel document in laravel 5.4+ you can use this way:

Route::domain('{account}.myapp.com')->group(function () {
    Route::get('user/{id}', function ($account, $id) {
        //
    });
});

A better answer found on Laravel discussion on the same topic is with macros. It works great for me. https://github.com/laravel/framework/issues/4017#issuecomment-267820459

Route::macro("domain", function(array $domains, \Closure $definition) {
    foreach ($domains as $domain) {
        Route::group(['domain' => $domain], $definition);
    }
});

Route::domain(['foo.bar.dev', 'foo.bar'], function($route) {
    // Do stuff
});

Currently you cannot. I had the same 'problem'; my fix is to cycle through your subdomains with a foreach and register the routes.

see this link. http://laravel.com/docs/routing#sub-domain-routing

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

or Use this package.

https://github.com/jasonlewis/enhanced-router

It help you can set where on group routing like this.

Route::group(array('domain' => '{maindomain}'), function()
{
    ...
})->where('maindomain', '.+\.example\.com$');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top