1

I am passing in some variables into a controller method. However the last one is not being recognized, even though dumping the variable on the twig side confirms the variable is not null.

Controller code

/**
     * @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
            ]
        );
    }

Twig template that passes in the variables

<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>

4
  • Nothing in your route's path configuration hints at provisionalTerm. That is probably why it's not read. Can you check what URL is generated in your template? You should probably either add the variable to the path, e.g. .../about/{provisionalTerm} or have a ParamConverter or ArgumentResolver that fetches the value from the request's additional parameters, e.g. with something like $request->query->get('provisionalTerm') and passes it to the controller action. Commented Jun 24, 2019 at 21:32
  • @dbrumann is there no way to pass in a variable without having it show up in the URL? I ideally would only want to show the ID in the URL. Commented Jun 24, 2019 at 21:34
  • I agree with @dbrumann. What does the generated URL look like? If you are really wanting to pass the provisionalTerm to your controller (though there's no sign of it in your controller), then how about adding ?provisionalTerm={{ tile.term.provisional}} after the path() term in your href definition. Commented Jun 25, 2019 at 1:02
  • You don't have to put it manually after path, every parameter not catch in the route definition is automatically added at the end of url. Then you get them from the request. Commented Jun 25, 2019 at 8:19

1 Answer 1

2

If you don't define provisionalTerm in your route path like:

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

Symfony will add your parameter (and any other not defined in the route path) at the end of url like this:

/courseSpace/116168444546/about?provisionalTerm=1

Then you can get them from the Request.

/**
         * @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
                ]
            );
        }
Sign up to request clarification or add additional context in comments.

3 Comments

If you don't have a custom argument resolver, query parameters are not mapped to arguments of the controller (method) being called.
So is there no way to pass in a variable to a controller route and have it not appear in the route? Other than using a hidden data field via HTML and JavaScript?
You can have a default value so you'll only have to pass it when the value is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.