jeff
jeff

Reputation: 1197

Symfony2 how to inject request and get route parameters in controller

My controller method looks following:

   /**
     * @Route("/film/{slugDe}", name="movie_De")
     */
    public function movieAction($slugDe)
    {

now I need to bind a form with request, but request is not injected. How do I inject request and keep the route parameters?

Upvotes: 2

Views: 2630

Answers (2)

ChadSikorra
ChadSikorra

Reputation: 2869

You could get the request like this from within the controller...

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

Edit:

Actually, after thinking about this a bit, I think Martin's answer might be the better route to take. While the above is a perfectly valid way to get the request from the controller, type-hinting for the request is probably the preferred method. And as shown, you can still type-hint for the request in your controllers action method when you're using slugs.

What is the best way to get the 'Request' object in the controller?

http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

Upvotes: 5

Martin
Martin

Reputation: 877

You can simply inject request like this:

use Symfony\Component\HttpFoundation\Request;

public function movieAction($slugDe, Request $request)
{
    // ...

    $form->handleRequest($request);

    // ...
}

See http://symfony.com/doc/current/book/controller.html#the-request-as-a-controller-argument

Upvotes: 2

Related Questions