Laravel 5有没有一种方法可以得到公认的前缀列表,比如['gbr','en','eu'],这样
/gbr/bar/baz/shoelace // or /eu/bar/baz/shoelace由与之相同的controller @方法处理
/bar/baz/shoelace除了在第一个条件中传递附加参数foo=gbr ?
请注意Route::group前缀不起作用,因为在这种情况下可能有也可能没有前缀。 此外,该策略应该优先于其他所有策略,即Route将首先检查(可选)前缀。
is there a way in Laravel 5 to have a recognized list of prefixes, such as ['gbr','en','eu'], so that
/gbr/bar/baz/shoelace // or /eu/bar/baz/shoelaceis handled by the same controller@method as
/bar/baz/shoelaceExcept that the additional parameter foo=gbr is passed in the first condition?
Note the Route::group prefix won't work because there may or may not be a prefix in this case. Also, this strategy should take precedence over all else, i.e. the Route would check for the (optional) prefix first.
最满意答案
是的,有一种方法。
在声明你的路线时,你可以声明它们为
Route::get('{prefix}/bar/baz/shoelace', 'controller@method')->where('prefix', 'gbr|en|eu');gbr|en|eu是一个简单的正则表达式,它将匹配字符串gbr, en or eu 。 查看正则表达式约束以获取更多细节
在你的控制器中,你可以拥有
public function method($prefix) { //code here }Yes there is a way.
When declaring your routes, you can declare them as
Route::get('{prefix}/bar/baz/shoelace', 'controller@method')->where('prefix', 'gbr|en|eu');gbr|en|eu is a simple regular expression that will match either the string gbr, en or eu. Check out Regular expression constraints for more details
And in your controller you can have
public function method($prefix) { //code here }更多推荐
发布评论