Symfony控制器方法无法识别传入的变量

时间:2019-06-24 21:06:35

标签: php symfony twig

我正在将一些变量传递到控制器方法中。但是,即使将变量转储到树枝侧上确认该变量为 not null。

控制器代码

/**
     * @Route(name = "course_space_view", path="/courseSpace/{courseSpaceId}/about", methods={"GET", "POST"})
     *
     * @ParamConverter("courseSpace", class="AppBundle\Entity\CourseSpace")
     * @ParamConverter("listSettings", class="AppBundle\View\ListEnrollment\ListSettings", options={"activeTab" = ListSettings::ABOUT_TAB})
     *
     * @param ListSettings $listSettings
     * @param CourseSpace $courseSpace
     * @param bool $provisionalTerm
     *
     * @return  Response
     */
    public function viewCourseSpace(ListSettings $listSettings, CourseSpace $courseSpace, bool $provisionalTerm)
    {
        $this->denyAccessUnlessGranted(CourseSpaceVoter::READ, $courseSpace);

        return $this->render('/LandingPage/CourseSpaces/aboutCourseSpace.html.twig',
            [
                'courseSpace' => $courseSpace,
                'listSettings' => $listSettings
            ]
        );
    }

传递变量的树枝模板

<a class="btn btn-sm btn-light"
   href="{{ path('course_space_view', {'courseSpaceId': tile.courseSpaceId, 'provisionalTerm': tile.term.provisionable}) }}">
     <span class="fas fa-cog mr-1"></span>
     Manage course space
</a>

1 个答案:

答案 0 :(得分:2)

如果您未在路径中定义临时条款,例如:

path="/courseSpace/{courseSpaceId}/about/{provisionalTerm}"

Symfony会将您的参数(以及其他未在路由路径中定义的参数)添加到url的末尾,如下所示:

/ courseSpace / 116168444546 / about?provisionalTerm = 1

然后您可以从请求中获取它们。

/**
         * @Route(name = "course_space_view", path="/courseSpace/{courseSpaceId}/about", methods={"GET", "POST"})
         *
         * @ParamConverter("courseSpace", class="AppBundle\Entity\CourseSpace")
         * @ParamConverter("listSettings", class="AppBundle\View\ListEnrollment\ListSettings", options={"activeTab" = ListSettings::ABOUT_TAB})
         *
         * @param ListSettings $listSettings
         * @param CourseSpace $courseSpace
         * @param bool $provisionalTerm
         *
         * @return  Response
         */
        public function viewCourseSpace(Request $request, ListSettings $listSettings, CourseSpace $courseSpace)
        {
            $this->denyAccessUnlessGranted(CourseSpaceVoter::READ, $courseSpace);
            $provisionnalTerm = $request->query->get('provisionalTerm');
            return $this->render('/LandingPage/CourseSpaces/aboutCourseSpace.html.twig',
                [
                    'courseSpace' => $courseSpace,
                    'listSettings' => $listSettings
                ]
            );
        }