Get post parameters in zend framework in put method

I get parameters using this

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

I found two ways to get the POST options

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

Both of these methods work in the "POST" method, but now in the "PUT" method, if I pass the values ​​as message parameters.

How can I get the message parameters in the "PUT" method?

+6
source share
3 answers

You need to read the request body and analyze it, for example:

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

This will analyze all the parameters in $putParams -array, so you can access it as if you had access to the super _ $_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; } 
+5
source

I think the correct way to do this is with 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 parameters via getParams ()

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

or simply

 $this->getRequest()->getParam("myvar"); 
+7
source

I'm having trouble using the PUT data sent from AngularJS and found that the best way is to use the custom Zend plugin

 class Web_Mvc_Plugin_JsonPutHandler extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { if (!$request instanceof Zend_Controller_Request_Http) { return; } if ($this->_request->isPut()) { $putParams = json_decode($this->_request->getRawBody()); $request->setParam('data', $putParams); } } } 

You can then access through getParams as a PHP object.

  $data = $this->getRequest()->getParam('data'); $id = $data->id; 
0
source

All Articles