Home > PHP Framework > Laravel > body text

How to use swagger in laravel 5.6

藏色散人
Release: 2021-03-04 09:04:58
forward
2760 people have browsed it

Using swagger in laravel 5.6How to use swagger in laravel 5.6

##When writing this article, My laravel version is
Install
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)
...
Copy after login
Run
php artisan vendor:publish
Copy after login
Select

L5Swagger\L5SwaggerServiceProvider

This

Two files will be added at this time

/config/l5-swagger.php
/resources/views/vendor/l5-swagger/index.blade.php

  • Configuration
  • Add a comment before
  • class
in the

app/Http/Controllers/Controller.php

file

<?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;
}
Copy after login
Run command
php artisan l5-swagger:generate
Copy after login
Copy after login
Open your project URLhttp://localhost/api/documentation, you will see that swagger has run successfully, but no API documentation is displayed.

Write documentation

Gethttp://localhost/home

Example:

index

method in HomeController Write the document

/**
     * @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');
    }
Copy after login
above and run the command again
php artisan l5-swagger:generate
Copy after login
Copy after login
Go back to http://localhost/api/documentation to refresh, the document will be out, it should look like this Appearance


How to use swagger in laravel 5.6 Recommended:

The latest five Laravel video tutorials

The above is the detailed content of How to use swagger in laravel 5.6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template