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