4

I have a very simple question, How can I get the POST values from get->Request();

public function emptytrashAction(){
    $request = $this->getRequest();
    $portfolio_id = $_POST["test"];
}

I dont want to use the $_POST variable and the form I submit just contain this hidden field test. The form is,

 <form name="empt_trash" action="{{ path('MunichInnovationGroupPatentBundle_portfolio_emptytrash') }}" method="post" >
    <input type="hidden" name="test" value={{ selected_portfolio.id }}>
    <input class="button3 tooltip" name = "submit" type="submit" value="Empty"></a>
 </form>

How can I get the value of the hidden field without using $_POST?

Edit

If a method use both GET and POST requests, For the Post request I check in my code like this

            if ($request->getMethod() == 'POST')

but it is not the symfony2 way then what is the proper way to check for the POST request?

j0k
  • 22,600
  • 28
  • 79
  • 90
Zoha Ali Khan
  • 1,659
  • 11
  • 37
  • 56

5 Answers5

8

As simple as :

$request  = $this->getRequest();
$postData = $request->request->get('test');

Note: This solution is only valid for Symfony <2.4 version. For 2.4 is deprecated and removed for 3.0.

The new code for obtain the request should be:

$request = $this->container->get('request_stack')->getCurrentRequest();
$postData = $request->request->get('test');
shakaran
  • 10,612
  • 2
  • 29
  • 46
j0k
  • 22,600
  • 28
  • 79
  • 90
3

$this->getRequest() is a deprecated method since symfony 2.4 and it will be removed in the version 3.0, so the best way to get the current request is through the following code:

//src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
...

/*
 * ...
 * @deprecated Deprecated since version 2.4, to be removed in 3.0. Ask
 *             Symfony to inject the Request object into your controller
 *             method instead by type hinting it in the method's signature.
 */
public function getRequest()
{
    return $this->container->get('request_stack')->getCurrentRequest();
}

Introduced by the following evolution,

[FrameworkBundle] use the new request_stack service to get the Request object in the base Controller class.

sami boussacsou
  • 1,063
  • 9
  • 18
1

You should not use getRequest() method its deprecated its always a good idea to inject request into your controller. Define you action like this:

public function emptytrashAction(Request $request){
    $portfolio_id = $request->request->get('test');
}

Never use GLOBALS like POST or GET in your actions they are not secure.

Here you can find more details: http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

Ashish Awasthi
  • 1,484
  • 1
  • 19
  • 34
  • if someone let me know the reasons of down vote it would be great. May be it will help me to make my answer more helpful and accurate. – Ashish Awasthi Oct 05 '15 at 04:36
  • Indeed, this is the proper solution! – Xavi Montero Dec 20 '20 at 15:59
  • In fact, the deprceation message seen in PhpUnit is clear and states exactly what Ashish said: `The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.` - So see that **the only reliable way is to inject** the Request itself. – Xavi Montero Dec 20 '20 at 16:02
0

Try this $this->getRequest()->request->get($path)

osm
  • 4,186
  • 3
  • 23
  • 24
0

As described in the Symfony2 Blog, you should use the Request object directly as a parameter of your methods in your controllers.

For a service, you can inject the object through the __construct method and store it in an internal property.

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

To determine if your request method was post

if ($request->isMethod('post')) {...}
Tim
  • 1,238
  • 1
  • 14
  • 24