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 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统