Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get post parameters in zend framework in "put" method

I am getting get parameters using this

$this->params()->fromQuery('KEY');

I found two way to get POST parameters

//first way
$this->params()->fromPost('KEY', null);

//second way
$this->getRequest()->getPost();

Both of this working in "POST" method but now in a "PUT" method if I pass values as a post parameters.

How I can get post parameters in "PUT" method?

like image 925
keen Avatar asked Jan 21 '14 07:01

keen


2 Answers

I guess the right way of doing that is by using Zend_Controller_Plugin_PutHandler:

// you can put this code in your projects bootstrap
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Zend_Controller_Plugin_PutHandler());

and then you can get your params via getParams()

foreach($this->getRequest()->getParams() as $key => $value) {
    ...
}

or simply

$this->getRequest()->getParam("myvar");
like image 171
npetrovski Avatar answered Nov 14 '22 22:11

npetrovski


You need to read the request body and parse it, something like this:

$putParams = array();
parse_str($this->getRequest()->getContent(), $putParams);

This will parse all params into the $putParams-array, so you can access it like you would access the super globals $_POST or $_GET. For instance:

// Get the parameter named 'id'
$id = $putParams['id'];

// Loop over all params
foreach($putParams as $key => $value) {
    echo 'Put-param ' . $key . ' = ' . $value . PHP_EOL;
}
like image 20
Kasper Pedersen Avatar answered Nov 14 '22 23:11

Kasper Pedersen