4

I have an embedded controller as described at Symfony's website. http://symfony.com/doc/current/templating/embedding_controllers.html

The only difference is that my controller has a form. All is rendered correctly but once the form is submitted, the request is always empty. Since the request is empty, the $form->isValid() and $form->isSubmitted() always return false.

Please find below my code:

Twig:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

Controller:

public function myEmbeddedAction(Request $request)
    {
      $template ="myTwig.html.twig";
      $supportTicket = new SupportTicket();
      $form = $this->createForm('AppBundle\Form\SupportTicketType', $supportTicket);
      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
        // success
      }
      return $this->render($template, array(
          'supportTicket' => $supportTicket,
          'form' => $form->createView()

      ));
}

Embedding the controller in my layout:

{{ render(controller('AppBundle:Default:myEmbedded')) }}

The request at the embedded action has always the same value:

{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}

However, if I access the embedded controller directly through the URL and fill the form, it works. How can I get the form to work inside of my embedded controller?

2
  • Just for grins try $request = $this->get('request_stack')->getMasterRequest(). When using embedded controllers you actually receive a sub request. I would have thought that the sub request will still have the master request attributes but maybe not. Commented Mar 4, 2017 at 20:35
  • Yes, it worked. Thanks a lot. Could you add as an answer so I can accept it? Commented Mar 4, 2017 at 20:39

1 Answer 1

6

Just for grins try

$request = $this->get('request_stack')->getMasterRequest(); 

When using embedded controllers you actually receive a sub request. I would have thought that the sub request will still have the master request attributes but I guess not.

Update: 2019-09-16

While the above code will still work when extending from AbstractController, it would be more in keeping with the spirit of Symfony to use injection:

public function myEmbeddedAction(RequestStack $requestStack)
{
    $request = $requestStack->getMasterRequest();
Sign up to request clarification or add additional context in comments.

1 Comment

it seems to be $requestStack->getMainRequest() in Symfony 6

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.