ApiBlueprint在laravel框架中的使用

说明

API Blueprint is simple and accessible to everybody involved in the API lifecycle. Its syntax is concise yet expressive. With API Blueprint you can quickly design and prototype APIs to be created or document and test already deployed mission-critical APIs
复制代码

通俗一点就是一种接口规范,而且是使用markdown的,使用markdown的语法书写接口。

背景

目前和前端联调、其他部门联调使用的方式有:

  1. 使用MarkDown出接口文档,放在共同的gitlab仓库上面,前后端都可以访问(只要约定好谁修改就好了,避免两个人都修改出现差异),作为一个经常写方法注释的好程序员来说(其实你的leader也会要求你),要在每一个接口上面写上几行方法注释,注明这个方法是做啥的,不然别人接手不便捷 ^_^;

  2. 使用apiDoc工具(使用方法可以查看:birjemin.com/wiki/php-ap…),将相应的接口规范以注释的方式写在每一个方法上面,然后生成相应的apiDoc文档(只需要写一下注释,不需要再写接口文档啦~),通过node服务搭建一个服务环境,前端直接访问我的开发机进行查看啦~

  3. 使用birjemin/blueprint+aglio/macdown...组合,比如在接口的方法上面按照一定的格式进行注释,然后使用该composer包生成apib文件,这个文件是一个遵循blueprint接口规范的markdown文件。可以使用aglio插件搭建一个web服务(这个插件支持实时更新,不需要刷新页面),也可以使用macdown编辑器查看这个文件。

搭建步骤

1.安装aglio(npm是啥???自己问前端同学吧。。)

npm install -g aglio
复制代码

请检查是否安装成功。

2.给laravel项目引入composer包(包已经提交,不过国内镜像还没同步)

composer require birjemin/blueprint dev-master
复制代码

or

composer require birjemin/blueprint
复制代码

3.在app.php中添加BlueprintServiceProvider::class

4.给接口添加注释,添加在Controller入口方法前面(详细的注释方式请查看github.com/dingo/api/w…)。

  /**
  * Class CarsController
  * @package App\Http\Controllers
  * @Resource("CarResource", uri="/api/cars")
  */
  class CarController extends Controller
  {
  /**
    * cars list
    *
    * Get current cars list
    *
    * @Get("/list")
    * @Transaction({
    *      @Request(identifier="page=1&type=1"),
    *      @Response(200, body={"msg": "返回成功","code": 200,"page": 1,"timestamp": "1522673813","data":{"result":[{"price": "2200","type": "福特","notice": "豪车"},{"price": "2200","type": "大众","notice": "车"}]}})
    * })
    * @Parameters({
    *      @Parameter("page", type="integer", required=true, description="分页", default=1),
    *      @Parameter("search", type="string", required=false, description="搜索条件"),
    *      @Parameter("type", type="integer", required=true, description="汽车类型", default=1, members={
    *          @Member(value="1", description="新车"),
    *          @Member(value="2", description="旧车")
    *      })
    * })
    */
    public function index()
    {
      return json_decode('{"succ":true,"code":0,"message":"","data":{"result":true}})');
    }
  }
复制代码

@Response(200, body={...}中的[]要替换成{},这里不替换是因为我的博客使用了jekyll,github报错啦~~

5.创建apib文件

php artisan birjemin:docs --output-file=tianming.apib
复制代码

项目下面生成了tianming.apib文件,这个是一个markdown文件,可以直接用markdown编辑器打开,下面讲一下aglio的web服务。

6.运行aglio服务(详细的命令可以去github.com/danielgtayl…查看)

aglio -i tianming.apib -s
复制代码

7.访问:http://27.0.0.1:3000(端口可以自定义,这个是默认的)

注意点

1.返回的数据不可以json如果是数组,{}代替[]这个符号,比如示例中的。

 {"msg": "返回成功","code": 200,"page": 1,"timestamp": "1522673813","data":{"result":[{"price": "2200","type": "福特","notice": "豪车"},{"price": "2200","type": "大众","notice": "车"}]}}
复制代码

就要将里面的中括号替换成大括号!

2.这些注释写错了会报错哦~

3.和apidoc的注释有区别~~

补充文档

参考

  1. apiblueprint.org/
  2. www.piaoyi.org/php/DingoAp…
  3. github.com/danielgtayl…
  4. github.com/dingo/bluep…
  5. github.com/dingo/api/w…
  6. github.com/monkeycraps…

转载于:https://juejin.im/post/5ac2529d6fb9a028cf32a42f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值