定制 itrnka/ha-smart-http-rest-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

itrnka/ha-smart-http-rest-api

Composer 安装命令:

composer require itrnka/ha-smart-http-rest-api

包简介

Rest API handling for `ha` framework. Package includes routes and controllers by ha framework standards. Currently is supported only JSON body in requests and responses.

README 文档

README

Rest API handling for ha framework. Package includes routes and controllers by ha framework standards. Currently is supported only JSON body in requests and responses.

How it works

For Rest API functionionality is required only one route, which is appended to ha router. This route is instance of ha\SmartHTTPRestAPI\JSON\JSONRestAPIRoute and maps requests by URL to required controllers.

Required area of functionality (product handling, category handling, ...) represent one controller, which implements ha\SmartHTTPRestAPI\JSON\JSONRestAPIController. All, what is needed, is add the controller into JSONRestAPIRoute route. This controller must implement ha\SmartHTTPRestAPI\JSON\JSONRestAPIController or must be extended from ha\SmartHTTPRestAPI\JSON\AbstractJSONRestAPIController (this abstract provides default required functionality for Rest API).

interface JSONRestAPIController

Associated route automatically maps matched request to these methods in controllers (every controller must implement interface):

interface JSONRestAPIController
{
    /**
     * Handle POST request without item id in URL - save item(s) defined in request body.
     * @param string $projectName
     */
    public function createItems(string $projectName): void;

    /**
     * Handle DELETE request with item id in URL. Please do not use body in your request.
     *
     * @param string $projectName
     * @param string $itemId Item id in string format from URL
     */
    public function deleteItem(string $projectName, string $itemId): void;

    /**
     * Handle DELETE request without item id in URL. Please do not use body in your request. Specification to delete
     * must be set in $_GET.
     * @param string $projectName
     */
    public function deleteItems(string $projectName): void;

    /**
     * Handle GET request with item id in URL.
     *
     * @param string $projectName
     * @param string $itemId Item id in string format from URL
     */
    public function getItem(string $projectName, string $itemId): void;

    /**
     * Handle GET request without item id in URL (e.g. search, items list, ...). Specification and filter must be set
     * in $_GET.
     * @param string $projectName
     */
    public function getItems(string $projectName): void;

    /**
     * Handle PUT request with item id in URL (this is idempotent!), save data from request body.
     *
     * @param string $projectName
     * @param string $itemId Item id in string format from URL
     */
    public function updateItem(string $projectName, string $itemId): void;

    /**
     * Handle PUT request without item id in URL (this is idempotent!), save data from request body.
     *
     * @param string $projectName
     */
    public function updateItems(string $projectName): void;
}

Example controller

<?php
declare(strict_types=1);

use ha\SmartHTTPRestAPI\JSON\AbstractJSONRestAPIController;

class ExampleAPIController extends AbstractJSONRestAPIController
{

    /**
     * Handle GET request with item id in URL.
     *
     * @param string $projectName
     * @param string $itemId Item id in string format from URL
     */
    public function getItem(string $projectName, string $itemId): void
    {
        /* TODO load and set data to $data by $id and $projectName via associated service, if item does not exists,
         * then throw new HTTP404Error();
         */
        $data = [
            'project' => $projectName,
            'itemData' => ['id' => $itemId, 'prop1' => 'item data here', 'prop2' => 'other item data here']
        ];
        $this->setResponseData(200, $data);
    }

    /**
     * Handle GET request without item id in URL (e.g. search, items list, ...). Specification and filter must be set
     * in $_GET.
     * @param string $projectName
     */
    public function getItems(string $projectName): void
    {
        // TODO load and set data to $data by $_GET filter and $projectName via associated service
        $data = [
            'project' => $projectName,
            'items' => [],
        ];
        $this->setResponseData(200, $data);
    }

}

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2017-11-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固