Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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() {     ... }); 
like image 994
Andy Fleming Avatar asked Sep 04 '13 00:09

Andy Fleming


People also ask

What is Domain grouping in router?

A network domain is an administrative grouping of multiple private computer networks or local hosts within the same infrastructure. Domains can be identified using a domain name; domains which need to be accessible from the public Internet can be assigned a globally unique name within the Domain Name System (DNS).

What is group routing in Laravel?

Route Groups is an essential feature in Laravel, which allows you to group all the routes. Routes Groups are beneficial when you want to apply the attributes to all the routes. If you use route groups, you do not have to apply the attributes individually to each route; this avoids duplication.

What are the two main routing files found in Laravel?

php and Modules/Media/backendRoutes. php files.

What is subdomain routing in Laravel?

by laravelrecipies. Subdomain routing is the same as routing prefixing, but it's scoped by subdomain instead of route prefix. There are two primary uses for this. First, you may want to present different sections of the application (or entirely different applications) to different subdomains.


2 Answers

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.

like image 70
Andy Fleming Avatar answered Oct 04 '22 13:10

Andy Fleming


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 () {   ... });  
like image 20
Johnathan Douglas Avatar answered Oct 04 '22 12:10

Johnathan Douglas