【发布时间】:2018-08-15 20:30:26
【问题描述】:
我从这里下载了 Live Helper Chat(一个使用 PHP 构建的开源应用程序):https://livehelperchat.com/,并且我一直在尝试将自定义端点添加到基于 Swagger 2.0 构建的 REST API。
到目前为止,我
1) 像这样在 swagger.json 中添加路径:
"/restapi/myendpoint/{user_id}": {
"get": {
"tags": [
"user"
],
"summary": "",
"description": "",
"produces": [
"application/json"
],
"parameters": [
{
"name": "user_id",
"in": "path",
"description": "User ID",
"required": true,
"type": "string",
"format": "int32"
}
],
"responses": {
"200": {
"description": "",
"schema": {
}
},
"400": {
"description": "Error",
"schema": {
}
}
},
"security": [
{
"login": []
}
]
}
}
2) 在 modules/lhrestapi 下创建了一个名为 myendpoint.php 的文件:
<?php
try {
erLhcoreClassRestAPIHandler::validateRequest();
$user = erLhcoreClassModelUser::fetch((int) $Params['user_parameters']
['user_id']);
if ($user instanceof erLhcoreClassModelUser) {
$db = ezcDbInstance::get();
echo json_encode(array('error' => false, 'result' => array('msg' =>
'Status changed', 'online' => $user->hide_online == 0)));
} else {
throw new Exception('User could not be found!');
}
} catch (Exception $e) {
echo erLhcoreClassRestAPIHandler::outputResponse(array(
'error' => true,
'result' => $e->getMessage()
));
}
exit();
和
3) 在 lhrestapi\module.php 中添加了这个:
$ViewList['myendpoint'] = array(
'params' => array('user_id')
);
当我尝试这个端点时,我得到代码 302(重定向到 /index.php/),就好像路由不存在一样。
据我从 API 中的其他端点可以看出,这些更改应该足以获得新的端点。
注意:我还尝试更改 swagger.json 中现有路径之一的路径,并且端点仍然可以使用旧路径。我开始认为我需要运行一些命令来更新 API,因为简单地编辑 swagger.json 文件似乎完全没有任何效果。另一方面,我很肯定我添加的代码中没有语法错误。
知道我做错了什么吗?
【问题讨论】:
标签: php rest api swagger swagger-2.0