use*_*329 4 laravel laravel-5.5
有没有办法创建带前缀的路线,所以我可以有这样的路线
/articles.html -> goes to listing Controller in default language
/en/articles.html -> goes to the same controller
/fr/articles.html -> goes to the same controller
Run Code Online (Sandbox Code Playgroud)
我目前的问题是通过这样做:
Route::group(['prefix=>'/{$lang?}/',function(){});
Run Code Online (Sandbox Code Playgroud)
像这样的路线:/authors/author-100.html将匹配前缀'authors`,并且肯定没有称为"作者"的语言.
我用laravel 5.5
使用可选路由参数上Regex匹配的位置应该足够了:
Route::get('/{lang?}, 'SameController@doMagic')->where('lang', 'en|fr');
Run Code Online (Sandbox Code Playgroud)
您也可以在路由组上执行相同的操作,否则具有此答案中的所有选项显然都有效.
显示前缀使用的更新:
Route::group(['prefix' => '{lang?}', 'where' => ['lang' => 'en|fr']],function (){
Route::get('', 'SameController@doNinja');
});
Run Code Online (Sandbox Code Playgroud)
就我而言,即使没有lang也没有lang,这应该足够了,也许这个群体可能会在其他路线之前到来.
似乎没有任何好的方法来使用可选前缀,因为带有“可选”正则表达式标记的组前缀方法不起作用。但是,可以使用所有路由声明一个Closure,并添加一次带有前缀而一次不添加:
$optionalLanguageRoutes = function() {
// add routes here
}
// Add routes with lang-prefix
Route::group(
['prefix' => '/{lang}/', 'where' => ['lang' => 'fr|en']],
$optionalLanguageRoutes
);
// Add routes without prefix
$optionalLanguageRoutes();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3710 次 |
| 最近记录: |