我希望能夠在我的應用程序中爲不同的域使用不同的路由。我想採取不同的行動,具體取決於域名是否爲Laravel路線組匹配整個域
- 我自己的域名例如
mysite.com/something
- 我的域的子域例如
subdomain.mysite.com/something
- 任何其他域例如
anotherdomain.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在我的hosts文件),我得到一個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
一樣。
感謝, 喬納森
無論誰投下了票,你會介意與我們分享原因嗎?這是正確使用本網站的方式。 – 2015-02-16 09:10:48
仔細閱讀問題並回答您通過本文解決問題中描述的問題? – 2015-06-26 04:56:31