dongpai2754 2018-08-15 20:30
浏览 72
已采纳

带有Swagger的PHP REST API - LiveHelperChat

I downloaded Live Helper Chat (an Open Source application built with PHP) from here: https://livehelperchat.com/, and I have been trying to add a custom endpoint to the REST API which is built on Swagger 2.0.

So far, I

1) Added the path in swagger.json like this:

    "/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) created a file named myendpoint.php under modules/lhrestapi :

<?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();

and

3) added this inside lhrestapi\module.php :

$ViewList['myendpoint'] = array(
'params' => array('user_id')
);

When I try this endpoint, I am getting code 302 (a redirect to /index.php/) as if the route doesn't exist.

As far as I can see from the other endpoints in the API, these changes should be enough to get a new endpoint.

Note: I also tried changing the path of one of the existing paths inside swagger.json, and the endpoint still works with the old path. I am starting to think there is some command that I need to run to update the API since simply editing the swagger.json file seems to have absolutely no effect whatsoever. On the other hand, I am positive there are no syntax errors in the code I added.

Any idea what I am not doing correctly?

  • 写回答

1条回答 默认 最新

  • drzk21632 2018-08-15 23:24
    关注

    Found the answer: when changes are made, the cache needs to be cleared or even disabled while developing. More details here: https://livehelperchat.com/article/view/40

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?