通配符前缀Laravel 5(Wildcard Prefix Laravel 5)

编程入门 行业动态 更新时间:2024-10-15 08:21:22
通配符前缀Laravel 5(Wildcard Prefix Laravel 5)

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/shoelace

is handled by the same controller@method as

/bar/baz/shoelace

Except 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 }

更多推荐

本文发布于:2023-07-28 06:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1301643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   前缀   Laravel   Wildcard   Prefix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!