Search code examples
twigsymfony4

Symfony4: render cannot pass parameters


After updating from Symfony 2.X to Symfony 4.4 and verifying the operation, I found that Twig does not recognize modal.
Since it does not recognize modal, it will be displayed even when the code that is not displayed in modal in the if statement is displayed in modal.
When I tried "if modal is empty", the code in the if statement was displayed, so it seems that the modal is not passed.
The code that passes the modal has changed with the Symfony update. This may have an effect.
Do you have any idea?

Old_code

    <div id="imageDialog" title="Image management">
        {{ render(controller("AppBundle:Shop/Image:manager", {"modal": true})) }}
    </div>

Changed_code

    <div id="imageDialog" title="Image management">
        {{ render(controller("AppBundle\\Controller\\Shop\\ImageController::managerAction", {"modal": true})) }}
    </div>

Code in question

    <div class="tabpanel selected" id="imageFolder" >
        <div class="tabcontent">
            <div class="operations">
                {% if not modal %}
                    <div class="pull-right">
                        <form method="post" action="{{ path('app_shop_image_delete') }}">
                            <input type="hidden" name="methods" value="delete">
                            <input type="hidden" name="_csrf_token" value="{{ csrf_token("authenticate") }}">
                            <input type="hidden" name="ids" value="[]">
                            <button type="submit" class="delete btn btn-danger"><i class="icon-trash"></i> Delete</button>
                        </form>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

ImageController

     *
     * @Method("GET")
     * @Route("/manager")
     *
     * @Template("@AppBundle/Shop/Image/manager.html.twig")
     */
    public function managerAction(Request $request)
    {
        $routeParams = $request->get('_route_params');
        $uploadUrl = $this->generateUrl("app_shop_image_save");
        return array(
            'modal' => isset($routeParams["modal"]) ? $routeParams["modal"] : false,
            'form'  => $this->createUploadForm($uploadUrl)->createView(),
        );
    }

Solution

  • It worked fine with the following method. I couldn't get $routeParams["modal"], but please let me know if there is a way to get it.

        public function managerAction(Request $request)
        {
            $routeParams = $request->query->get('modal');
            $uploadUrl = $this->generateUrl("app_hq_image_save");
            return array(
                'modal' => isset($routeParams) ? $routeParams : false,
                'form'  => $this->createUploadForm($uploadUrl)->createView(),
            );
        }
    
         <div id="imageDialog" title="Image management">
             {{ render(controller("AppBundle\\Controller\\Shop\\ImageController::managerAction", {}, {"modal": true})) }}
         </div>