通过注解定义的路由中的可选参数 [英] optional parameters in routes defined through annotations
本文介绍了通过注解定义的路由中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更优雅的方式在带注释的路由中定义可选参数然后定义 2 个注释?
is there a more elegant way to define optional parameters in annotated routes then to define 2 annotations?
我是这样做的:
/**
*
* @Route("/view/{lang}/{file}", name="legacy_translation_view_file")
* @Route("/view/{lang}", name="legacy_translation_view")
* @Template()
*/
public function viewAction($lang,$file=null)
{
...
}
我已经看到注释类有一个名为defaults"的字段,但我不确定语法
i've seen that the annotation class has a field named "defaults" but am not quiet sure about the syntax
谢谢
推荐答案
Symfony 在 @Route:
Symfony has a page on @Route:
例如,也许你可以试试.
E.g maybe you can try.
/**
* @Route("/{id}/{lang}/{file}", requirements={"id" = "\d+"}, defaults={"file" = null})
*/
public function showAction($id, $lang, $file)
{
}
如果 null 不起作用,请尝试使用空字符串.
If null doesn't work try an empty string.
这篇关于通过注解定义的路由中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文