路线中间的可选参数

编程入门 行业动态 更新时间:2024-10-26 18:23:19
本文介绍了路线中间的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在路线中间添加可选参数?

Is there any way to add an optional parameter in the middle of a route ?

示例路线:

/things/entities/ /things/1/entities/

我尝试过此方法,但是它不起作用:

I tried this, but it does not work:

get('things/{id?}/entities', 'MyController@doSomething');

我知道我可以做到...

I know I can do this...

get('things/entities', 'MyController@doSomething'); get('things/{id}/entities', 'MyController@doSomething');

...但是我的问题是:我可以在路线中间添加可选参数吗?

... but my question is: Can I add an optional parameter in the middle of a route?

推荐答案

否.可选参数需要放在路由的末尾,否则路由器将不知道如何将URL与路由匹配.您已经实现的是正确的方法:

No. Optional parameters need to go to the end of the route, otherwise Router wouldn't know how to match URLs to routes. What you implemented already is the correct way of doing that:

get('things/entities', 'MyController@doSomething'); get('things/{id}/entities', 'MyController@doSomething');

您可以尝试通过一条路线进行操作:

You could try doing it with one route:

get('things/{id}/entities', 'MyController@doSomething');

,如果要获取所有实体的实体,请传递 *或0 ,但我称其为hack.

and pass * or 0 if you want to fetch entities for all things, but I'd call it a hack.

还有其他一些骇客可以让您使用一种途径,但这会增加代码的复杂性,这确实不值得.

There are some other hacks that could allow you to use one route for that, but it will increase the complexity of your code and it's really not worth it.

更多推荐

路线中间的可选参数

本文发布于:2023-07-14 22:14:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   路线   参数

发布评论

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

>www.elefans.com

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