[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.前言 在平常浏览网页时,我们会注意到在有的网站中,当点击页面中的链接进行路由跳转时,页面顶部会有一个进度条,用来标示页面跳转的进度(如下图所示).虽然实际用处不大,但是对用户来说,有个进度条会大大 ...
随机推荐
- easyui 使用问题积累
一.<a>标签中onclick和javascript事件与grid的select事件执行的先后顺序 html: <a href='javascript:func();' onclic ...
- KindleEditor上传文件报404
初步怀疑是iis配置的允许上传大小太小了,然后就修改了配置文件但是不起作用. 后来百度了下iis版本是7.5,然后就按照iis8 的配置: IIS8请求筛选模块被配置为拒绝超过请求内容长度,在&quo ...
- javascript学习第五课this、call、apply
js函数与其它 高级语言相比有一个特点.没有返回值,一个简单函数就是function关键字+函数名字构成 this 对象是在运行中基于函数的执行环境绑定的,在全局函数中,this等于window,而当 ...
- Trie 最长前缀匹配串的实现
http://blog.csdn.net/hguisu/article/details/8131559
- 【java】:通用小知识
1.将String字符串放在最前面 为了防止偶发性的NullPointerException 异常,我们通常将String放置在equals()函数的左边来实现字符串比较,如下代码: // Bad i ...
- 对于大数据量的Json解析
近几天做了一个项目,需要解析大量的json数据,有一万多条,以前我用的都是Gson包去自动解析,但是速度真是不敢恭维,于是我又去查了其它的方法,发现fastjson的解析,发现速度直的是很快,在此我不 ...
- <数据结构与算法>之字符串,散列,布隆过滤器。
1:字符串 字符串是一组由数字,字符,下划线的一串字符,是特殊的一维数组. 2:字符串的应用 字符串移位包含问题: 例:给定两个字符串s1和s2,要求判断s2是否能被s1做循环移位得到字符串包含.例如 ...
- node3
- Python爬虫入门案例:获取百词斩已学单词列表
百词斩是一款很不错的单词记忆APP,在学习过程中,它会记录你所学的每个单词及你答错的次数,通过此列表可以很方便地找到自己在记忆哪些单词时总是反复出错记不住.我们来用Python来爬取这些信息,同时学习 ...
- CentOS常用的文件操作命令
CentOS下面常用的文件操作命令: cd pwd NO1. 显示当前路径 [root@rehat root]# pwd NO2. 返回用户主目录 [root@rehat root]# cd NO3. ...