gpt4 book ai didi

php - Laravel 路由组匹配整个域

转载 作者:可可西里 更新时间:2023-10-31 22:54:20 32 4
gpt4 key购买 nike

我希望能够在我的应用程序中针对不同的域使用不同的路由。我想根据域是否是不同的行为

  • 我自己的域,例如mysite.com/something
  • 我的域的子域,例如subdomain.mysite.com/something
  • 任何其他域,例如另一个域名.com

我是这样处理问题的:

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

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

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

代码:

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

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

throw new NotFoundHttpException;
}

有没有一种方法可以通过这种方式匹配任何不是我的域或我域的子域的域?之后我也需要能够访问该域以对其执行某些操作,就像我对 $subdomain 所做的那样。

谢谢,乔纳森

最佳答案

也在找这个...

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

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

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

关于php - Laravel 路由组匹配整个域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26783770/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com