[英]How to catch optional argument in laravel 5.2 route
I want create a general route to catch the optional lang argument in my laravel 5.2 routes. 我想创建一个通用路由来捕获我的laravel 5.2路由中的可选lang参数。
Currently I do it with this route 目前我用这条路线做
//language switcher
Route::get('lang/{lang}/{suffix}', function($lang, $suffix) {
Session::set('applocale', $lang);
return Redirect($suffix);
});
So, if you preceed your url with an optional "/lang/[lang-code]" router will change applocale and redirect to the resource. 因此,如果您在网址前加上可选的“/ lang / [lang-code]”路由器,则会更改applocale并重定向到资源。
However I would like to make in a cleaner way with regular expressions, someting like: 但是我想用一个更清晰的方式使用正则表达式,有些像:
//language switcher
Route::get('(es|ca|en)/{suffix}', function($lang, $suffix) {
Session::set('applocale', $lang);
return Redirect($suffix);
});
But don't know how.. 但不知道如何..
As always say: RTM! 一如既往地说:RTM!
Solution: 解:
// generic language switcher
// catch any route preceded with "es" ,"ca" or "en", set the applocale and
// redirect to suffix route
Route::get('{lang}/{suffix}', function($lang, $suffix) {
Session::set('applocale', $lang);
return Redirect($suffix);
})
->where(['lang' => '(es|ca|en)', 'suffix' => '(.*)']);
https://laravel.com/docs/5.2/routing#parameters-regular-expression-constraints https://laravel.com/docs/5.2/routing#parameters-regular-expression-constraints
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.