[英]Laravel: Optional route prefix parameter
I'm currently working on a multi-site application (one codebase for multiple (sub)sites) and I would love to leverage route caching, but currently I'm hardcoding a prefix instead of dynamically determining it. 我目前正在开发一个多站点应用程序(多个(子)站点的一个代码库),我很乐意利用路由缓存,但目前我正在硬编码前缀而不是动态确定它。
When trying to do this I'm running into an issue which I've illustrated below: 当我试图这样做时,我遇到了一个问题,我在下面说明了这个问题:
Route::group(['prefix' => '{subsite}', 'subdomain' => '{site}.domain.tld'], function () {
Route::get('blog', 'BlogController@index')->name('blog.index');
});
When accessing a subsite like http://sitename.domain.tld/subsitename/blog
this all works fine, but it doesn't work anymore when not accessing a subsite like http://sitename.domain.tld/blog
, as it will now think that the prefix is 'blog'. 当访问像http://sitename.domain.tld/subsitename/blog
这样的子网站时,一切正常,但是当不访问像http://sitename.domain.tld/blog
这样的子网站时,它不再起作用了,因为它现在会认为前缀是'博客'。
Is there any way to allow the 'subsite' parameter to be empty or skipped? 有没有办法让'subsite'参数为空或跳过?
Thanks! 谢谢!
As far as I know there isn't anything in the current routing system that would allow you to solve your problem with a single route group. 据我所知,目前的路由系统中没有任何内容可以让您解决单个路由组的问题。
While this doesn't answer your specific question, I can think of two ways that you could implement your expected behaviour. 虽然这不能回答您的具体问题,但我可以想到两种方法可以实现您的预期行为。
Route::group(['subdomain' => '{site}.domain.tld'], function () {
Route::get('blog', 'BlogController@index')->name('blog.index');
});
Route::group(['prefix' => '{subsite}', 'subdomain' => '{site}.domain.tld'], function () {
Route::get('blog', 'BlogController@index')->name('blog.index');
});
$prefixes = ['', 'subsiteone', 'subsitetwo'];
foreach($prefixes as $prefix) {
Route::group(['prefix' => $prefix, 'subdomain' => '{site}.domain.tld'], function () {
Route::get('blog', 'BlogController@index')->name('blog.index');
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.