[英]How can I have optional parameters in Symfony2 route?
I have this code below:我在下面有这个代码:
/**
* Lists all User entities.
*
* @Route("/{cid}",defaults={"cid" = null},name="user")
* @Template()
*/
public function indexAction($cid=null)
{}
Now if I type site/user/1
then it works, but if I type site/user/
it says:现在,如果我输入
site/user/1
那么它就可以工作,但是如果我输入site/user/
它会说:
No route found
How can I have it that both routes work?我怎么能知道两条路线都有效?
Try to go to site/user
(notice no backslash at the end).尝试转到
site/user
(注意末尾没有反斜杠)。
Generally it should work, I have relatively similar configuration working.一般来说它应该可以工作,我有相对相似的配置工作。
But if all else fails you can always define multiple routes for same action, ie但是,如果所有其他方法都失败了,您始终可以为同一操作定义多个路由,即
/**
* Lists all User entities.
*
* @Route("/", name="user_no_cid")
* @Route("/{cid}", name="user")
* @Template()
*/
public function indexAction($cid=null)
{
You could also do it with a GET parameter, eg你也可以用一个 GET 参数来做,例如
/**
* @param Request $request
*
* @return Response
*/
public function displayDetailAction(Request $request) : Response
{
if ($courseId = $request->query->get('courseId')) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.