我想在Zend Framework 1中实现RESTful设计。我不确定如何记录资源以及允许哪些方法以及可以提供哪些额外参数。
我当前的实现使用控制器名称作为资源,HTTP方法是操作名称。我已经发送了#34;允许"在标题中包含:
GET, PUT, PATCH, LINK, UNLINK, POST, DELETE, HEAD, OPTIONS
我想知道如何记录客户端的可能和必需参数。
现在我正在编写DocBlock属性并将其发送到" OPTIONS"请求。
/**
* @route /data/results
* @method GET
* @requiredParams pid The project ID
* @queryParams tid The team ID
*/
public function getAction()
{
$pid = $this->_getParam('pid');
$teamId = $this->_getParam('tid');
if (null === $pid) {
$this->getResponse()->setHttpResponseCode(400);
$this->getResponse()
->setBody(Zend_Json::encode(array('required parameters missing')));
} else {
$this->getResponse()->setBody(Zend_Json::encode(array($pid, $teamId)));
$this->getResponse()->setHeader('Content-Type', 'application/json');
$this->getResponse()->setHttpResponseCode(200);
}
}
public function optionsAction()
{
$reflect = new Zend_Reflection_Class($this);
foreach (explode(',',$this->getAllowedMethods()) as $method) {
$action = strtolower(trim($method)) . 'Action';
try {
$docs[] = $method.' '.$reflect->getMethod($action)
->getDocblock()->getContents();
} catch(Zend_Reflection_Exception $e) {
}
}
$this->getResponse()->setBody(implode("\n",$docs));
$this->getResponse()->setHttpResponseCode(200);
}