在"put"方法中获取zend框架中的post参数

kee*_*een 6 php zend-framework put post-parameter zend-framework2

我正在使用此获取参数

$this->params()->fromQuery('KEY');
Run Code Online (Sandbox Code Playgroud)

我找到了两种获取POST参数的方法

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

//second way
$this->getRequest()->getPost();
Run Code Online (Sandbox Code Playgroud)

这两个都在"POST"方法中工作,但现在在"PUT"方法中,如果我将值作为post参数传递.

如何在"PUT"方法中获取后置参数?

npe*_*ski 7

我想正确的方法是使用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());
Run Code Online (Sandbox Code Playgroud)

然后你可以通过getParams()得到你的参数

foreach($this->getRequest()->getParams() as $key => $value) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

或者干脆

$this->getRequest()->getParam("myvar");
Run Code Online (Sandbox Code Playgroud)


Kas*_*sen 5

您需要阅读请求正文并解析它,如下所示:

$putParams = array();
parse_str($this->getRequest()->getContent(), $putParams);
Run Code Online (Sandbox Code Playgroud)

这会将所有参数解析为$putParams-array,因此您可以访问它,就像访问超级全局$_POST$_GET.例如:

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

// Loop over all params
foreach($putParams as $key => $value) {
    echo 'Put-param ' . $key . ' = ' . $value . PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)