[Laravel-Swagger]如何在 Laravel 项目中使用 Swagger
如何在 Laravel 项目中使用 Swagger
http://swagger.io/getting-started/
安装依赖 swagger-php
composer require zircote/swagger-php
创建 SwaggerController,用于为了 swagger-php 提供 json 数据
php artisan make:controller SwaggerController
给 SwaggerController 加上 SwaggerJSON 数据的处理:
SwaggerController.php
use Illuminate\Http\Request;
use Swagger\Annotations as SWG;
/**
* @SWG\Swagger(
* schemes={"http","https"},
* host="api.host.com",
* basePath="/",
* @SWG\Info(
* version="1.0.0",
* title="This is my website cool API",
* description="Api description...",
* termsOfService="",
* @SWG\Contact(
* email="contact@mysite.com"
* ),
* @SWG\License(
* name="Private License",
* url="URL to the license"
* )
* ),
* @SWG\ExternalDocumentation(
* description="Find out more about my website",
* url="http..."
* ),
* // define tag
* @SWG\Tag(name="Home", description="Roote Route"),
* @SWG\Tag(name="User", description="UserController"),
* @SWG\Tag(name="Role", description="RoleController"),
* @SWG\ExternalDocumentation(
* description="Find out more about my website",
* url="http..."
* ),
* @SWG\Definition(
* definition="errorModel",
* required={"status code", "message"},
* @SWG\Property(
* property="status code",
* type="integer",
* format="int32"
* ),
* @SWG\Property(
* property="message",
* type="string"
* )
* ),
* // 定义 API
* @SWG\Definition(
* definition="Login",
* @SWG\Property(
* property="useraccount",
* type="string"
* ),
* @SWG\Property(
* property="password",
* type="string"
* )
* ),
* @SWG\Definition(
* definition="logout",
* @SWG\Property(
* property="token",
* type="string"
* )
* ),
* )
*/
class SwaggerController extends Controller
{
public function doc()
{
$swagger = \Swagger\scan(realpath(__DIR__.’/../../’));
return response()->json($swagger);
}
}
具体 UserController.php 中 Swagger Definition
/**
* @SWG\Post(
* path="/user/login",
* summary="Sign in",
* tags={"User"},
* operationId="Login",
* description="Login",
* produces={"application/json"},
* @SWG\Parameter(
* name="account & password",
* in="body",
* description="Login",
* required=true,
* @SWG\Schema(ref="#/definitions/Login"),
* ),
* @SWG\Response(
* response=200,
* description="It's ok"
* ),
* @SWG\Response(
* response="default",
* description="unexpected error",
* @SWG\Schema(ref="#/definitions/errorModel")
* )
* )
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function login( Request $request)
{
$arr_user_info = [];
$ret = $this->validateLogInfo($request, $arr_user_info);
if ($ret) {
return $ret;
}
$ret = $this->verifyLogInfo($arr_user_info);
if ($ret) {
return $ret;
}
}
在 Laravel 中使用 Swagger 出现报错总结:
- ErrorException in Logger.php line 38: Required @SWG\Info() not found
方法: use Swagger\Annotations as SWG;
The problem is - you're missing the @SWG\Info block. It's a block which tells swagger some most common information about your API.
I usually put this in a separate controller which is rendering swagger JSON. Here's an example:
还必须在类定义的前面,添加上下面的这段:
- Can't read swagger JSON
检查 JSON 接口 URL 时候正确
安装使用参考教程:https://segmentfault.com/a/1190000004980342
[Laravel-Swagger]如何在 Laravel 项目中使用 Swagger的更多相关文章
- 如何在spring-boot web项目中启用swagger
swagger的三个项目及其作用 我们打开swagger的官网,会发现有三个swagger相关的项目,它们分别是 swagger-editor 作用是通过写代码,生成文档描述(一个json文件或其他格 ...
- 如何在cocos2d项目中enable ARC
如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...
- 如何在NodeJS项目中优雅的使用ES6
如何在NodeJS项目中优雅的使用ES6 NodeJs最近的版本都开始支持ES6(ES2015)的新特性了,设置已经支持了async/await这样的更高级的特性.只是在使用的时候需要在node后面加 ...
- 如何在VUE项目中添加ESLint
如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...
- 如何在mvc项目中使用apiController
文章地址:How do you route from an MVC project to an MVC ApiController in another project? 文章地址:How to Us ...
- 如何在Ionic2项目中使用第三方JavaScript库
onic的官网放出一记大招Ionic and Typings,来介绍如何在Ionic2项目中使用第三方JavaScript库. 因为在前阵子正好想用一个非常有名的第三方JS库ChartJs来实现一些东 ...
- 如何在maven项目中使用spring
今天开始在maven项目下加入spring. 边学习边截图. 在这个过程中我新建了一个hellospring的项目.于是乎从这个项目出发开始研究如何在maven项目中使用spring.鉴于网上的学习资 ...
- 如何在Vue-cli项目中使用JTopo
1.前言 jTopo(Javascript Topology library)是一款完全基于HTML5 Canvas的关系.拓扑图形化界面开发工具包.其体积小,性能优异,由一群开发爱好者来维护.唯一感 ...
- 如何在Vue项目中给路由跳转加上进度条
1.前言 在平常浏览网页时,我们会注意到在有的网站中,当点击页面中的链接进行路由跳转时,页面顶部会有一个进度条,用来标示页面跳转的进度(如下图所示).虽然实际用处不大,但是对用户来说,有个进度条会大大 ...
随机推荐
- redis对比其余数据库
Redis属于常见的NoSQL数据库或者说非关系数据库:Redis不使用表,她的数据库也不会预定义或者强制去要求用户对Redis存储的不同数据进行关联. 常见数据库对比: 和高性能键值缓存服务器mem ...
- jQuery的input 失去焦点之后,不能再获取到焦点
今天写了一个字段唯一性校验功能,验证设备仪器编号唯一,当输入编号之后 ,点击其他,失去焦点后,后台验证唯一,有过此编号,就给出提示,重新填写. 当使用ie时候,获取焦点正常 ,但是使用火狐就获取不到焦 ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...
- How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)
Question: On CentOS 7, I want to switch from DHCP to static IP address configuration with one of my ...
- 遇到IIS7配置PHP出现403和404错误的解决办法
服务器要配置PHP,总是出现403错误.服务器是新装的,操作系统是windows server 2008 R2,装的IIS7. IIS里PHP和本地服务器对比了好几遍,都没到出错的原因,后来通过cmd ...
- Knockout.Js官网学习(value绑定)
前言 value绑定是关联DOM元素的值到view model的属性上.主要是用在表单控件<input>,<select>和<textarea>上. 当用户编辑表单 ...
- postgresql数据迁移
postgresql从库故障准备新库 1,创建用户[root@localhost home]# userdel postgres[root@localhost home]# groupdel post ...
- 前端tip
background简写 参考地址 http://css.doyoe.com/ 缩写:background:url(test1.jpg) no-repeat scroll 10px 20px/50px ...
- MySQL 5.7系列之sys schema(2)
0.导读 MySQL 5.7引入了sys schema,有了它,我们排查分析一些问题时将更得心应手.sys schema里主要存储的是视图.存储过程.函数等. 视图:用于结果的汇总展示及配置持久化: ...
- vios 多 vlan设置
[转 ]测试后修正 成功让IVM跑了多个VLAN,添加一块可携带多个vlanID的网卡,Nativevlan是改造的折中方法,如果不喜欢这种方法,附---删除多vlan网卡方法,一般情况下,lpar的 ...