這篇文章主要介紹在laravel 5.6 中怎樣使用swagger,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Laravel 是一套簡潔、優雅的PHP Web開發框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構建一個完美的網絡APP,而且每行代碼都可以簡潔、富于表達力。
在laravel 5.6 中使用swagger
寫本篇文章時,本人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
中刷新,文檔就已經出來了,應該是長這個樣子
以上是“在laravel 5.6 中怎樣使用swagger”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。