The use of swagger in the Laravel project

Source: Internet
Author: User
Tags documentation

This article describes how to quickly introduce swagger into the laravel and start using it. installation

The first is a composer:l5 swagger using Laravel.
The following are the corresponding versions.

Laravel Swagger UI openapi Spec Compatibility L5-swagger
5.1.x 2.2 1.1, 1.2, 2.0 PHP composer require "Darkaonline/l5-swag ger:~3.0 "
5.2.x 2.2 1.1, 1.2, 2.0 PHP composer require" Dar kaonline/l5-swagger:~3.0 "
5.3.x 2.2 1.1, 1.2, 2.0 php compo Ser require "darkaonline/l5-swagger:~3.0"
5.4.x 2.2 1.1, 1.2, 2.0 PHP composer require "darkaonline/l5-swagger:~3.0"
5.4.x 3 2.0 PHP composer require "darkaonline/l5-swagger:5.4.*"
5.5.x 3 2.0 PHP composer require "darkaonline/l5-swagger:5.5.*"

The following procedures are all based on Laravel 5.5.x
Execute the command under the Laravel root directory
Composer require "darkaonline/l5-swagger:5.5.*"
Wait for the installation to finish.
Then execute: Run PHP artisan l5-swagger:publish to initialize run PHP artisan l5-swagger:generate to generate environment variables into the. env file

Now you can see that there is a l5-swagger.php in the Config directory, which is the corresponding swagger configuration.

Then start the project and visit your website address/api/documentation to see the initialization interface.

Next, write a swaggercontroller in the controller, where the code is as follows:

<?php

namespace App\http\controllers;

Use Swagger\annotations\info;

/**
 * @Info (
 *     title= "My title",
 *     version= "v1.0.0"
 *)
 /
class Swaggercontroller extends Controller
{
    /**
     * Assumption is an API in the project
     *
     * @SWG \get (path= "/swagger/") My-data ",
     *   tags={" Project "},
     *   summary=" Take some mysterious data ",
     *   description=" Request this interface to log in first. " ",
     *   operationid=" Getmydata ",
     *   produces={" Application/json "},
     *   @SWG \parameter (
     *     in= "FormData",
     *     name= "Reason",
     *     type= "string",
     *     description= " Reason to take the data ",
     *     required=true,
     *   ),
     *   @SWG \response (response=" Default ", description=" Operation Successful ")
     * *
     )
    /Public Function getmydata ()
    {

    }
}

This is the re-access http://localhost/api/documentation, you can see the following screen:

Now, start using swagger.

Enclosed is the demo address of swagger-php, which can be referenced in the example.
swagger-php Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.