使用多域路由时,Laravel将域添加到命名路由之前 [英] Laravel prepending domain to named routes when using multiple domain routing
问题描述
由于我无法评论这另一个问题由于声誉,我再次询问.
Since I couldn't comment on this other question because of reputation, I am asking it again.
就像OP发布的一样,Laravel的web.php文件中的路由如下:
Like the OP had posted, the routes in the web.php file for Laravel are as follows:
$loginRoutes = function () {
Route::get('/', 'HomeController@index')->name('home');
};
Route::domain('domain1.com')->group($loginRoutes);
Route::domain('domain2.com')->group($loginRoutes);
Route::domain('localhost')->group($loginRoutes);
在刀片中调用命名的路由时,例如,使用route('home')时,上面代码最后一行中的域位于链接之前.
When calling named routes in blade, for example using route('home') the domain in the last line item of the code above is prepended to the link.
因此,如果我们位于domain1.com上,并且刀片中的链接引用route('home'),则该URL将以http://localhost作为域.
So if we are on domain1.com and a link in blade references route('home') the URL will have http://localhost prepended as the domain.
如何避免不对URL进行硬编码就避免这种情况?
How can I avoid this without going through and hardcoding the urls?
更新我已经将两种方法结合在一起(作为对可能在此发生的其他人的答案发布),但我希望有人可以提供一些更好的方法来解决此问题.
Update I have hacked to together two approaches (posted as answers for others who may happen along here), but I hope someone can provide some clarity to a better way to handle this.
推荐答案
好的,在此处发布第三个选项.
Alright, posting yet a third option here.
同样,我会在此处为可能遇到该帖子的任何人发布此选项.我仍在寻找一种可行且更优雅的解决方案.
Again, I am posting this option here for anyone that may come across the post. I am still looking for a viable and more elegant solution.
$LARAVEL_HTTP_HOST = explode(':',$_SERVER['HTTP_HOST'])[0];
$loginRoutes = function () {
Route::get('/', 'HomeController@index')->name('home');
};
$allowedDomains = array(
'domain1.com',
'domain2.com',
'localhost',
);
if (in_array($LARAVEL_HTTP_HOST,$allowedDomains))
{
Route::group(['domain' => $LARAVEL_HTTP_HOST], $loginRoutes);
}
这篇关于使用多域路由时,Laravel将域添加到命名路由之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!