Symfony2 Route只在整数时获取参数

Ste*_*sic 3 symfony symfony-routing

我在routing.yml文件中定义了路由

一条路线是:

Profile_user_profile:
    path:    /profile/{id}
    defaults: { _controller: ProfileBundle:Users:profile }
    methods: [get]
Run Code Online (Sandbox Code Playgroud)

第二是:

Profile_accept_connection_proposal:
    path:    /profile/acceptProposal
    defaults: { _controller:ProfileBundle:Users:acceptConnectionProposal }
    methods: [put]
Run Code Online (Sandbox Code Playgroud)

没有方法的第一条路线:[get]听取并[put]请求并在它到达路由定义之前捕获第二个url.如果url是数字,是否有定义检查参数的方法.

gp_*_*ver 6

只需添加requirements参数以仅接受确定路线的数字,如下所示:

Profile_user_profile:
    path:    /profile/{id}
    defaults: { _controller: ProfileBundle:Users:profile }
    methods: [get]
    requirements: <--- ADDED PARAMETER
        id: \d+
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请阅读有关路由Symfony书籍.在那里,您可以找到有关如何使用路径参数的更高级示例.


cha*_*arj 5

您现在可以在控制器中使用 Annotations 执行此操作,如下所示:

class UserController extends AbstractController
{
    /**
     * @Route("/profile/{id}", name="user_profile", requirements={"id"="\d+"})
     */
    public function profile($id)
    {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

有关Symfony 文档的更多信息 具体 定义路由要求