可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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()
{
...
});
回答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.
回答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 () {
...
});
回答3:
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 :)
回答4:
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()
{
...
});
回答5:
Currently you cannot. I had the same 'problem'; my fix is to cycle through your subdomains with a foreach and register the routes.
回答6:
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) {
//
});
});
回答7:
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.
回答8:
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$');