
--------上圖 截取自Github 官網上的安裝參考-----------------------------------------------------------------------------------------------------------------------------------------------------------
本人只用到了Laravel5.5.x, 所以只講此版本的安裝,所有步驟如下
①步驟1:
在你搭建的laravel項目根目錄中執行命令: composer require "darkaonline/l5-swagger:5.5.*"
②步驟2:
繼續在項目根目錄中執行命令: php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"
執行該命令后,會在config目錄下產生一個l5-swagger.php的配置文件,里面包含了swagger標題,token驗證,路由等常用的基本配置
其中’generate_always’配置可以根據需要修改下,它表示是否每次都刷新swagger, 示例如下:
'generate_always' => env('L5_SWAGGER_GENERATE_ALWAYS', false), // 把false改為true,你每次修改了配置它就會馬上更新, 否則你更改了配置它不會馬上生效
③步驟3:
在項目根目錄中執行命令: composer require 'zircote/swagger-php:2.*'
然后啟動項目,瀏覽器訪問測試:你的網站地址/api/documentation 可以看到swagger的界面了,但會提示缺少api-docs.json文件。
需在項目app下創建個php文件(注意:我是直接放在app文件夾下,放在app/http的Controllers文件夾下也可以),命名為:swagger.php 文件內容如下:
<?php
/**
* Class Controller
*
* @package App\Http\Controllers
*
* @SWG\Swagger(
* basePath="",
* host="127.0.0.1",
* schemes={"http"},
* @SWG\Info(
* version="1.0",
* title="OpenApi",
* @SWG\Contact(name="Pek Ratanak", url="https://www.google.com"),
* ),
* @SWG\Definition(
* definition="Error",
* required={"code", "message"},
* @SWG\Property(
* property="code",
* type="integer",
* format="int32"
* ),
* @SWG\Property(
* property="message",
* type="string"
* )
* )
* )
*/
④步驟4:在項目根目錄中執行命令: php artisan l5-swagger:generate // 如果上面不創建swagger.php 如果執行此命令就會拋出錯誤: In Logger.php line 38: Required @SWG\Info() not found 切忌!
現在重新訪問 你的網站地址/api/documentation 刷新swagger就可以了 如圖:

在app/http/Controllers文件夾下ApiController.php代碼如下:
<?php
/**
* Created by PhpStorm.
* User: compter
* Date: 2018/8/30
* Time: 15:17
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ApiController extends Controller
{
/**
* @SWG\GET(
* path="/api/index",
* summary="api index by tags",
* tags={"測試"},
* description="返回測試內容",
* operationId="apIndex",
* produces={"application/json"},
* @SWG\Parameter(
* name="tags",
* in="query",
* description="拿數據的理由",
* required=true,
* type="string",
* ),
* @SWG\Response(
* response=200,
* description="Dashboard overview."
* ),
* @SWG\Response(
* response=401,
* description="Unauthorized action.",
* )
* )
*/
public function index(Request $request)
{
return response()->json([
'result' => [
'statistics' => [
'users' => [
'name' => 'Name',
'email' => '213213@qq.com'
]
],
],
'message' => '',
'type' => 'success',
'status' => 0
]);
}
}
添加laravel的路由中 Route::get('/api/index','ApiController@index');
重新訪問刷新頁面即可 ----------我的預覽圖如下

以下為get方法的一些案例演示
<?php
/**
* Created by PhpStorm.
* User: compter
* Date: 2018/8/30
* Time: 15:17
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ApiController extends Controller
{
//前端測試用接口
public function a () {
return [
'name' => '急急急',
'sex' => 18,
];
}
/**
* @SWG\GET(
* path="/api/index",
* summary="接口簡介",
* tags={"接口標簽,可以是多個"},
* description="接口描述,支持 Markdown 語法",
* operationId="操作的ID,需要唯一",
* produces={"application/json"},
* @SWG\Parameter(
* name="tags",
* in="query",
* description="拿數據的理由",
* required=true,
* type="string",
* ),
* @SWG\Response(
* response=200,
* description="Dashboard overview."
* ),
* @SWG\Response(
* response=401,
* description="Unauthorized action.",
* )
* )
*/
public function index(Request $request)
{
return response()->json([
'result' => [
'statistics' => [
'users' => [
'name' => 'Name',
'email' => '213213@qq.com'
]
],
],
'message' => '',
'type' => 'success',
'status' => 0
]);
}
}

<?php
/**
* 我的主頁面
* User: computer
* Date: 2018/8/31
* Time: 15:21
*/
namespace App\Http\Controllers\MySelf;
use App\Http\Controllers\Controller;
use App\Models\Accountee;
use App\Models\Channel;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
/**
* @SWG\Get(
* path="/my_self/home/index",
* summary="我的主頁面",
* tags={"我的主頁面"},
* description="我的主頁面",
* operationId="home.index",
* produces={"application/json"},
* @SWG\Response(
* response=200,
* description="基本信息",
* @SWG\Schema(
* type="json",
* @SWG\Property(
* property="channel_id",
* type="integer",
* description="資產渠道id"
* ),
* @SWG\Property(
* property="telephone",
* type="string",
* description="手機號"
* ),
* @SWG\Property(
* property="short_name",
* type="string",
* description="企業簡稱"
* )
* )
* ),
* @SWG\Response(
* response=422,
* description="error",
* )
* )
*/
public function index()
{
$channel=Channel::find(100033,['id','telephone','short_name']);
$channel['telephone']=substr_replace($channel->telephone,'*****',3,5); // 手機號脫敏
return $channel;
}
}

<?php
/**
* 用戶資料管理
* User: computer
* Date: 2018/8/31
* Time: 15:21
*/
namespace App\Http\Controllers\MySelf;
use App\Http\Controllers\Controller;
use App\Models\InfoPerson;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* @SWG\Get(
* path="/my_self/user/index",
* summary="用戶資料管理",
* tags={"用戶列表"},
* description="用戶資料列表",
* operationId="user.index",
* produces={"application/json"},
* @SWG\Parameter(
* name="page",
* in="query",
* description="分頁編號,默認1",
* type="integer",
* ),
* @SWG\Parameter(
* name="pagesize",
* in="query",
* description="每頁顯示條數,默認10",
* type="integer",
* ),
* @SWG\Response(
* response=200,
* description="用戶列表",
* @SWG\Schema(
* type="json",
* @SWG\Property(
* property="pages",
* @SWG\Property(
* property="totalnum",
* type="integer",
* description="記錄總數",
* ),
* @SWG\Property(
* property="totalpage",
* type="integer",
* description="總頁數",
* ),
* @SWG\Property(
* property="pagesize",
* type="integer",
* description="每頁顯示記錄數",
* ),
* @SWG\Property(
* property="page",
* type="integer",
* description="當前頁數",
* ),
* ),
* @SWG\Property(
* property="lists",
* @SWG\Items(
* @SWG\Property(
* property="id",
* type="integer",
* description="id",
* ),
* @SWG\Property(
* property="source",
* type="string",
* description="數據來源",
* ),
* @SWG\Property(
* property="industry_no",
* type="integer",
* description="行業編號",
* ),
* @SWG\Property(
* property="channel_id",
* type="integer",
* description="渠道id",
* ),
* @SWG\Property(
* property="name",
* type="string",
* description="名稱",
* ),
* )
* ),
* ),
* ),
* @SWG\Response(
* response=422,
* description="error",
* )
* )
*/
public function index(Request $request){
$channel_id=2;
$page = $request->input('page',1);
$pagesize = $request->input('pagesize',PAGE_SIZE);
$offset = ($page - 1) * $pagesize;
$condition = InfoPerson::where('channel_id',$channel_id)->orderBy('id', 'desc');
$total = $condition->count();
if (empty($total)) abort(422, '沒有對應的數據');
$lists = $condition->offset($offset)->limit($pagesize)->get();
return [
'pages' => [
'totalnum' => $total,
'totalpage' => intval(ceil($total / $pagesize)),
'pagesize' => $pagesize,
'page' => $page,
],
'lists' => $lists
];
}
}

記得都要添加路由哦!至於post api有空再寫...
