我有一个使用CakePHP编写的PHP REST API作为项目的一部分。所有API端点都作为控制器中的单个方法存在,并接受JSON字符串中的参数和返回值。我想弄清楚我应该如何记录这些方法的参数和返回类型phpDocumentor2。如何在phpDocumentor 2中记录JSON参数和返回类型?
例如,如果我在UsersController有一个编辑()方法,对于用户模型,其骨架更新指定的字段看起来像这样(我已经简化为简洁的代码):
public function edit() {
//Get arguments
$args = $this->request->data['args'];
$id = $args['id'];
//Perform processing
if (!$this->User->exists($id)) {
$data = $this->createError(300);
}
else {
$this->User->id = $id;
$saveData = array();
if (isset([$args['first_name'])) {
$saveData['User']['first_name'] = $args['first_name'];
}
if (isset([$args['last_name'])) {
$saveData['User']['last_name'] = $args['last_name'];
}
$isSaved = $this->User->save($saveData);
if (count($this->User->validationErrors) > 0) {
$data = $this->createError(202, $this->User->validationErrors);
}
else {
$data = array('status' => $isSaved ? 1 : 0);
}
}
//Output data
return $data;
}
我可能返回以下JSON请求修改用户的姓氏和名字:
{
"id": 1
"first_name": "John"
"last_name": "Doe"
}
如果API调用成功,该方法将返回:
{
"status": 1
}
而如果不成功,也许是由于失败的数据验证,该方法可能会返回类似:
{
"status": 0
"code": 202,
"messages": {
"first_name": {
"Numeric characters are not allowed."
}
}
}
我明白,我可以使用的phpDocumentor的@return和@param分别记录返回值和参数,但从文档中,没有任何关于JSON返回的内容。
我可以举例来说,文档返回类型为
@return $value string A JSON string that contains the status code and error messages if applicable.
但我几乎认为适当,特别是涉及更复杂的数据结构返回(想象类似的东西Twitter的状态/ user_timeline),特别是对于“得到“和”查看“API方法。
在另一方面,参数,我不知道这是否是正确的,我来为每个参数(考虑到所有的参数都包裹在一个JSON字符串)这样一行:
@param string $id The ID of the user to be updated.
@param string $first_name optional The first name of the user.
@param string $last_name optional The last name of the user.
如果phpDocumentor无法满足此需求,我愿意探索其他选项 - 只是建议!
纠正我,如果我错了,但养蜂场似乎不允许单个字段的文档 - 它只是显示样本请求和响应的机构?我们已经有了一个API测试控制台,允许开发人员使用完整的工作样本来测试API,但需要一种手段来记录各个领域的细节(例如数据类型,可选/必需和说明)。 –
是的,apiary.io不支持你这样的需求。我向你展示蜂房,因为我认为你没有API控制台,而我只是通过它有我的示例API。我认为您需要像[Google API](https://developers.google.com/youtube/v3/docs/channels/list)这样的API控制台,它还会显示关于您之前说过的字段的文档。但是,如果您的API控制台不支持记录您的API,则可以使用WIKI制作关于您的API的文档/信息。我已经在我的项目中这样做了。我认为这比使用源代码文档更好。 –