如何使用Symfony将参数传递给控制器​​?

Ran*_*ngh 2 php controller symfony

我正在将var argurmrnt传递给这样的控制器

return $this->redirect($this->generateUrl('user_profile', array('var' => $profile->getId())));
Run Code Online (Sandbox Code Playgroud)

这是我的控制者

/**


         * @Route("/profile", name="user_profile")
         * @Template()
         */
        public function ProfileAction($var)
        {
            $em = $this->getDoctrine()->getEntityManager();
Run Code Online (Sandbox Code Playgroud)

但我不断收到错误

控制器"xxxx\Controller\UserController :: ProfileAction()"要求您为"$ var"参数提供一个值(因为没有默认值,或者因为在此之后存在非可选参数).

Vit*_*ian 8

根据文档,你忘了在你的路线上添加占位符

    /**
     * @Route("/profile/{var}", name="user_profile")
     * @Template()
     */
    public function ProfileAction($var)
    {
        $em = $this->getDoctrine()->getEntityManager();
Run Code Online (Sandbox Code Playgroud)