Zoha Ali Khan Zoha Ali Khan - 1 month ago 7
PHP Question

get->Request in Symfony2

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 j0k
Answer

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');