[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.前言 在平常浏览网页时,我们会注意到在有的网站中,当点击页面中的链接进行路由跳转时,页面顶部会有一个进度条,用来标示页面跳转的进度(如下图所示).虽然实际用处不大,但是对用户来说,有个进度条会大大 ...
随机推荐
- Finders Keepers
function find(arr, func) { //var num = 0; //return num; var res = arr.filter(func); if(res.length){ ...
- MVC4 本地正常运行,发布到IIS7->403 - 禁止访问: 访问被拒绝。
代码编写完成,计划发布一个版本测试,没想到发布到IIS7 竟然报错“403-禁止访问”.还真第一次遇到这种问题..... 折腾了半天,终于解决. 1.提示报错403: 禁止访问: 访问被拒绝.您无权使 ...
- sql 将查询结果为多行一列合并为一行一列
使用sql stuff函数 /* stuff(param1, startIndex, length, param2)说明:将param1中自startIndex(SQL中都是从1开始, ...
- 實際案例: 已知要獲取臨時票証 (JsApi Ticket) 才能調用的接口
需獲取票証才能調用的接口,簡單列示如下: 一.基礎類 1. wx.checkJsApi (當前客戶端是否支持指定JS) 二.分享類 1.wx.onMenuShareTimeline (分享到朋友圈)2 ...
- PHP数组合并 array_merge 与 + 的差异
在PHP数组合并出过几次问题都没记住,写下来加强一点记忆 PHP数组在合并时,使用 array_merge 与 + 的差异: 1.array_merge(array $array1 [, array ...
- 用Navicat_SSH 连接数据库服务器
SSH设置(只限于Mysql.oracle.PostgreSQL及 SQL Server 并只支持 SSH2通讯协定) Secure SHell(SSH)是一个通过网络登录其他计算机的程序,在远程服务 ...
- mb_系列函数和普通字符函数的区别
<?php //phpinfo(); $str = 'abcdef'; echo strlen($str);// 6 echo '<br/>'; echo substr($str, ...
- wcf http 代理
两个我都还没试,先记录着,其实我也不咋懂,所以记录着,权当一个线索 第一种 wcf中设置代理是在bing类中设置的,比如 WSHttpBinding ws = new WSHttpBinding(); ...
- 用python画xy散点图
import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6],'ro') plt.show()#这个智障的编辑器 这样的话,就可以画一个散点图,图中 ...
- 【java】:多表关联、排序
SELECT * FROM (SELECT coll.collection_id, coll.report_id, coll.new_id, news.title,news.abstract_txt, ...