php 我可以在 Laravel 的路由组中对多个域进行分组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18603330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:54:26  来源:igfitidea点击:

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

phplaravelroutinglaravel-routing

提问by Andy Fleming

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

回答by Andy Fleming

Laravel does not seem to support this.

Laravel 似乎不支持这一点。

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.

我不确定这个解决方案是否有任何显着的性能影响。

回答by Johnathan Douglas

Laravel 5.1

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

回答by Dylan Baskind

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

这个也有兴趣!我正在尝试为一个控制器操作注册本地开发 + 生产子域路由。

i.e.

IE

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

不是一个大问题,正如 DesignerGuy 提到的,我可以将一个函数传递给两条路线 - 但如果可以将它们分组会更优雅:)

回答by Marty

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.

以防万一您需要知道使用哪个域名调用控制器。使用 Laravel 5.6 对其进行了测试。

回答by Bradley

check in laravel docs, if you main domain is myapp, in production is myapp.comand in local environment is myapp.devtry using a *

检查laravel docs,如果您的主域是myapp,则在生产中是myapp.com,在本地环境中myapp.dev尝试使用*

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

回答by Winner1

according to laravel documentin laravel 5.4+ you can use this way:

根据laravel 5.4+中的laravel文档,您可以使用这种方式:

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

回答by Rob Gordijn

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

目前你不能。我有同样的问题'; 我的解决方法是使用 foreach 循环浏览您的子域并注册路由。

回答by EThaizone Jo

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

看到这个链接。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

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$');

回答by Dipu Raj

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

在关于同一主题的 Laravel 讨论中找到的更好的答案是使用宏。这对我很有效。 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
});