寫這篇文章時,本人laravel版本為5.6.28
安裝
composer require darkaonline/l5-swagger Using version ^5.6 for darkaonline/l5-swagger ... - Installing swagger-api/swagger-ui (v3.17.4) - Installing doctrine/annotations (v1.6.0) - Installing zircote/swagger-php (2.0.13) - Installing darkaonline/l5-swagger (5.6.5) ...
運行
php artisan vendor:publish
選擇L5Swagger\L5SwaggerServiceProvider
這項
這時會加入兩個檔案
- /config/l5-swagger.php
- #/resources/views/vendor/l5-swagger/index.blade.php
#配置
在app/Http/Controllers/Controller.php
檔案中class
前加入註解
<?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; /** * @SWG\Swagger( * basePath="/calculate-rates", * @SWG\Info( * title="项目名称 API", * version="1.0.0" * ) * ) */ class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; }
執行指令
php artisan l5-swagger:generate
開啟你的專案網址http://localhost/api/documentation
,你會看到swagger已經運作成功了,但沒有顯示任何API文件。
寫文件
拿http://localhost/home
範例:
在HomeController
的index
方法上面編寫文檔
/** * @SWG\Get( * path="/home", * summary="用户资料", * @SWG\Response(response=200, description="请求成功"), * @SWG\Response(response=401, description="用户验证失败"), * @SWG\Response(response=500, description="服务器错误") * ) * */ public function index() { return view('home'); }
再次運行命令
php artisan l5-swagger:generate
回到http://localhost/api/documentation
中刷新,文檔就已經出來了,應該是長這個樣子