Laravel 路由组匹配整个域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26783770/
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-09-14 10:23:35  来源:igfitidea点击:

Laravel route group match whole domain

phplaravellaravel-4dnsrouting

提问by Jonathon

I would like to be able to use different routes within my application for different domains. I would like to act differently depending on whether the domain is

我希望能够在我的应用程序中为不同的域使用不同的路由。我想根据域是否为不同的方式采取不同的行动

  • My own domain e.g. mysite.com/something
  • A subdomain of my domain e.g. subdomain.mysite.com/something
  • Any other domain e.g. anotherdomain.com
  • 我自己的域,例如 mysite.com/something
  • 我的域的子域,例如 subdomain.mysite.com/something
  • 任何其他域,例如 anotherdomain.com

I've approach the problem like this:

我已经解决了这样的问题:

// Match my own domain
Route::group(['domain' => 'mysite.com'], function()
{
    Route::any('/', function()
    {
        return 'My own domain';
    });
});

// Match a subdomain of my domain
Route::group(['domain' => '{subdomain}.mysite.com'], function()
{
    Route::any('/', function($subdomain)
    {
        return 'Subdomain ' . $subdomain;
    });
});

// Match any other domains
Route::group(['domain' => '{domain}'], function()
{
    Route::any('/', function()
    {
        return 'Full domain ';// . $domain;
    });
});

The first two groups work perfectly. Visiting mysite.comdisplays My own domainand visiting subdomain.mysite.comdisplayed Subdomain subdomainas expected. However, when I visit with anotherdomain.com(I have this set up as an alias in my vhost file as well as pointing it to the loopback IP in my hosts file), I get an NotFoundHttpException:

前两组工作完美。访问mysite.com显示My own domain和访问按预期subdomain.mysite.com显示Subdomain subdomain。但是,当我访问时anotherdomain.com(我在我的 vhost 文件中将此设置为别名,并将其指向我的主机文件中的环回 IP),我得到一个NotFoundHttpException

/var/www/portfolio/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php

Code:

代码:

    $others = $this->checkForAlternateVerbs($request);

    if (count($others) > 0)
    {
        return $this->getOtherMethodsRoute($request, $others);
    }

    throw new NotFoundHttpException;
}

Is there a way I can match any domain that is not my domain or a subdomain of my domain in this way? I need to be able to access the domain to do something with it afterwards too, just like I did with $subdomain.

有没有办法以这种方式匹配不是我的域或我的域的子域的任何域?之后我也需要能够访问域以对其进行处理,就像我对$subdomain.

Thanks, Jonathon

谢谢,乔纳森

回答by Spekkie

Was searching for this too...

也在找这个。。。

Probably not the most beautiful solution, but it works

可能不是最漂亮的解决方案,但它有效

// Match my own domain
Route::group(['domain' => 'mysite.com'], function()
{
    Route::any('/', function()
    {
        return 'My own domain';
    });
});

// Match a subdomain of my domain
Route::group(['domain' => '{subdomain}.mysite.com'], function()
{
    Route::any('/', function($subdomain)
    {
        return 'Subdomain ' . $subdomain;
    });
});

// Match any other domains
Route::group(['domain' => '{domain}.{tld}'], function(){

    Route::any('/', function($domain, $tld){
        return 'Domain: ' . $domain . '.' . $tld;
    });
});

Route::group(['domain' => '{subdomain}.{domain}.{tld}'], function(){

    Route::any('/', function($sub, $domain, $tld){
        return 'subdomain: ' . $sub . '.' . $domain . '.' . $tld;
    });
});

By the way, if you like to test it, add some fake domains to your HOSTS file, and point them to 127.0.0.1 :-)

顺便说一句,如果您想测试它,请在您的 HOSTS 文件中添加一些假域名,并将它们指向 127.0.0.1 :-)

回答by Sigismund

I think that isn't possible because you have declared your domain in config/app.php.

我认为这是不可能的,因为您已经在 config/app.php 中声明了您的域。

You could try creating separate environment for second domain.

您可以尝试为第二个域创建单独的环境。

You can view how to use second domain in Laravel in this tutorial.

您可以在本教程中查看如何在 Laravel 中使用第二个域。

回答by joelennon

This is a late response, but you can do dynamic full domain routing without resorting to ugly extension hacks. By default, the Laravel domain filter applies a regexp that prevents you from using period characters in the value passed to the filter. Hence why it works when you break it out into segments, but not if you try to pass the full domain as a single parameter.

这是一个迟到的响应,但您可以进行动态的完整域路由,而无需诉诸丑陋的扩展黑客。默认情况下,Laravel 域过滤器应用了一个正则表达式,以防止您在传递给过滤器的值中使用句点字符。因此,当您将其分解为多个段时它会起作用,但如果您尝试将整个域作为单个参数传递则不起作用。

The easiest solution is to modify the pattern applied to the domain filter in the bootmethod of your app/Providers/RouteServiceProvider.phpfile as follows:

最简单的解决方案是在boot您的app/Providers/RouteServiceProvider.php文件的方法中修改应用于域过滤器的模式,如下所示:

public function boot(Router $router)
{
    $router->pattern('domain', '[a-z0-9.]+');
    parent::boot($router);
}

This will allow you to send full domains through the filter.

这将允许您通过过滤器发送完整的域。

回答by Ghazanfar Mir

Looks like the question is old. However, you could use the following:

看起来这个问题很老了。但是,您可以使用以下内容:

Just remove the group part from the code as this will apply to any other domain

只需从代码中删除组部分,因为这将适用于任何其他域

Route::any('/', function()
{
    return 'Full domain ';// . $domain;
});