Symfony布线注释要求不起作用 路由.yml 控制器
当我访问www.mysite.com/app_dev.php/thisroute/to/asdfasdf时,由于要求设置为整数,当不应该访问时,它是可访问的 在我的线路中,我有一条线路 _主要内容: 资源:routing.ymlSymfony布线注释要求不起作用 路由.yml 控制器,symfony,routing,annotations,Symfony,Routing,Annotations,当我访问www.mysite.com/app_dev.php/thisroute/to/asdfasdf时,由于要求设置为整数,当不应该访问时,它是可访问的 在我的线路中,我有一条线路 _主要内容: 资源:routing.yml 对于注释设置,应为: /** * Template Select * @Route("/to/{id}", name="thisismyroute", requirements={"id"="\d+"}) * @Method({"POST"}) */ publ
对于注释设置,应为:
/**
* Template Select
* @Route("/to/{id}", name="thisismyroute", requirements={"id"="\d+"})
* @Method({"POST"})
*/
public function createRoutePage($id)
{
/* code here */
}
更新
对于yml配置,请尝试
thisismyroute:
path: thisroute/to/{id}
defaults: { _controller: MyBundle:Default:createRoutePage }
requirements:
id: \d+
通常,在控制器中使用@Route注释时,将routing.yml配置为一次性导入控制器目录中注释的所有路由会更容易,如下所示:
# import routes from a controller directory
route_group_name:
resource: "@YourBundle/Controller"
type: annotation
就这样。这样,您就不需要在routing.yml中指定每个路由
然后,您只能在控制器中使用注释:
/**
*
* @Route("/to/{id}", name="thisismyroute", requirements={"id"="\d+"})
* @Method({"POST"})
*/
public function myRouteAction($id)
{
/* code here */
}
PS:使用注释声明路由也是Symfony推荐的最佳实践。是的,我也尝试了“id”=“\d+”在不应该的时候仍然进入页面,我没有任何其他路由去/去/
# import routes from a controller directory
route_group_name:
resource: "@YourBundle/Controller"
type: annotation
/**
*
* @Route("/to/{id}", name="thisismyroute", requirements={"id"="\d+"})
* @Method({"POST"})
*/
public function myRouteAction($id)
{
/* code here */
}