1 Star 0 Fork 26

异想天开 / yii2-fast-api

forked from deepziyu / yii2-fast-api 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

yii2-fast-api

yii2-fast-api是一个Yii2框架的扩展,用于配置完善Yii2,以实现api的快速开发。 此扩展默认的场景是APP的后端接口开发,因此偏向于实用主义,并未完全采用restfull的标准,方便前端开发处理接口数据以及各种异常。

Installation

使用 Composer 安装

  • 在项目中的 composer.json 文件中添加依赖:
"require": {
    "deepziyu/yii-fast-api": "*"
}
  • 执行 $ php composer.phar update$ composer update 进行安装。

  • 在配置文件中( Yii2 高级版为 main.php,Yii2 基础版为 web.php )注入 fast-api 的配置:

// $config 为你原本的配置
$config = yii\helpers\ArrayHelper::merge(
    $config,
    \deepziyu\yii\rest\Controller::getConfig()
);

return $config;

Usage

  • 建立控制器
class YourController extends deepziyu\yii\rest\Controller
{
    /**
     * 示例接口
     * @param int $id 请求参数
     * @return string version api版本
     * @return int yourId 你的请求参数
     */
    public function actionIndex($id)
    {
        return ['version'=>'1.0.0','yourId'=>$id];
    }
}
  • 发送请求看看

正常请求

POST /your/index HTTP/1.1
Host: yoursite.com
Content-Type: application/json

{"id":"10"}

返回

{
    "code": 200,
    "data": {
        "version": "1.0.0",
        "yourId": "10"
    },
    "message": "OK"
}

缺少参数的请求

POST /your/index HTTP/1.1
Host: yoursite.com
Content-Type: application/json

返回错误

{
    "code": 400,
    "data": {},
    "message": "缺少参数:id"
}
  • 查看自动生成的Api文档

http ://yoursite.com/route/api/index

mahua

Words In The End

感谢@暗夜在火星 的PhalApi项目,为此Yii2扩展提供设计的思路。

TODO

  • 更完善的文档指南
  • Signature 过滤器插件
  • 限流插件的使用
  • RequestID 以及日志存储追踪的参考
Copyright (c) 2017, deepziyu All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the {organization} nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

快速配置并开发基于yii2的api服务 展开 收起
PHP
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/ucmir/yii2-fast-api.git
git@gitee.com:ucmir/yii2-fast-api.git
ucmir
yii2-fast-api
yii2-fast-api
master

搜索帮助

14c37bed 8189591 565d56ea 8189591