[英]Symfony2 Route get parameter only if integer
I got defined routes in routing.yml file 我在routing.yml文件中定义了路由
one route is: 一条路线是:
Profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Users:profile }
methods: [get]
and second is: 第二是:
Profile_accept_connection_proposal:
path: /profile/acceptProposal
defaults: { _controller:ProfileBundle:Users:acceptConnectionProposal }
methods: [put]
First route without methods: [get] listen also and [put] request and catch second url before it get to route definition. 没有方法的第一条路线:[get]听取并[put]请求并在它到达路由定义之前捕获第二个url。 Is there way to define checking for parameter only if url is numeric.
如果url是数字,是否有定义检查参数的方法。
Just add the requirements
parameter to accept only digits for a determined route like this: 只需添加
requirements
参数,只接受确定路径的数字,如下所示:
Profile_user_profile:
path: /profile/{id}
defaults: { _controller: ProfileBundle:Users:profile }
methods: [get]
requirements: <--- ADDED PARAMETER
id: \d+
For more infos read the Symfony book about Routing . 有关更多信息,请阅读有关路由的Symfony书籍 。 There you can find more advanced example on how to use route parameters.
在那里,您可以找到有关如何使用路径参数的更高级示例。
You can now do this with Annotations in your controller like so: 您现在可以在控制器中使用Annotations执行此操作,如下所示:
class UserController extends AbstractController
{
/**
* @Route("/profile/{id}", name="user_profile", requirements={"id"="\d+"})
*/
public function profile($id)
{
// ...
}
}
More info on Symfony's docs Specifically defining routing requirements 有关Symfony文档的更多信息专门定义路由要求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.