寫本篇文章時,本人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
php artisan vendor:publish
選擇L5Swagger\L5SwaggerServiceProvider
這項
這時會添加兩個文件laravel
在app/Http/Controllers/Controller.php
文件中class
前添加註釋git
<?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; }
運行命令github
php artisan l5-swagger:generate
打開你的項目網址http://localhost/api/documentation
,你會看到swagger已經運行成功了,可是沒有顯示任何API文檔。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'); }
再次運行命令app
php artisan l5-swagger:generate
回到http://localhost/api/documentation
中刷新,文檔就已經出來了,應該是長這個樣子composer
更多文檔編寫方法可參考 https://github.com/zircote/sw...ide