3

In a controller action we can get the request object in 2 ways:

$request = $this->getRequest();

or by passing it as a parameter to an action

public function testAction(Request $request)

What is the difference between them? Is a recommended way to get the request or specific situations when each is preferred?

zuzuleinen
  • 2,604
  • 5
  • 38
  • 49
  • the difference is that `testAction()` cannot be called without request on second way – Royal Bg Jan 07 '14 at 10:46
  • http://stackoverflow.com/questions/6916324/access-post-values-in-symfony2-request-object This thread might help you :) – sas Jan 07 '14 at 10:48

1 Answers1

4

Actually there is no visible difference before Symfony 2.4. But according to the documentation (https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md) you should pass request object as an action method argument.

The getRequest method of the base Controller class has been deprecated since Symfony 2.4 and must be therefore removed in 3.0. The only reliable way to get the Request object is to inject it in the action method.

Cyprian
  • 11,174
  • 1
  • 48
  • 45