6

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?

keen
  • 3,001
  • 4
  • 34
  • 59

3 Answers3

9

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");
npetrovski
  • 181
  • 1
  • 7
  • it gives an error that "front controller not found". Do i need to import any file to use this? – keen Jan 30 '14 at 12:06
  • it should be included by your Zend's autoloader. If it is not, I suppose you should include Zend\Constroller\Front and Zend\Controller\Plugin\PutHandler into your code – npetrovski Feb 14 '14 at 08:52
6

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;
}
Kasper Pedersen
  • 516
  • 3
  • 17
  • what this will return ? and How to get specific param from the list of parameters ? – keen Jan 21 '14 at 08:56
0

I had trouble using PUT data sent from AngularJS and found the best way was to use a 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);
        }
    }
}

Which can then be accesses via getParams as a PHP object

    $data = $this->getRequest()->getParam('data');
    $id = $data->id;
bstoney
  • 6,594
  • 5
  • 44
  • 51