简体   繁体   English

Laravel路由组匹配整个域

[英]Laravel route group match whole domain

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 eg mysite.com/something 我自己的域名,例如mysite.com/something
  • A subdomain of my domain eg subdomain.mysite.com/something 我的域的subdomain.mysite.com/something域名,例如subdomain.mysite.com/something
  • Any other domain eg anotherdomain.com 任何其他域名,例如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.com displays My own domain and visiting subdomain.mysite.com displayed Subdomain subdomain as expected. 访问mysite.com会显示My own domain并且访问subdomain.mysite.com会按预期显示Subdomain subdomain 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 : 但是,当我访问anotherdomain.com (我将此设置为我的vhost文件中的别名以及将其指向我的hosts文件中的环回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 谢谢你,乔纳森

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

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中使用第二个域。

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. 默认情况下,Laravel域过滤器应用正则表达式,以防止您在传递给过滤器的值中使用句点字符。 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. 因此,当您将其分解为段时,它为什么会起作用,但如果您尝试将完整域作为单个参数传递则不会。

The easiest solution is to modify the pattern applied to the domain filter in the boot method of your app/Providers/RouteServiceProvider.php file as follows: 最简单的解决方案是在app/Providers/RouteServiceProvider.php文件的boot方法中修改应用于域过滤器的模式,如下所示:

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. 这将允许您通过过滤器发送完整的域。

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM