Swagger is a very useful API documentation tool that greatly reduces the cost of communication between the front-end and the backend. When writing back-end interfaces, you can also use it to test the interface, which is very convenient.
The whole process is as follows. Create a new Laravel project
Composer Create-project--prefer-dist Laravel/laravel Blog
Integrated Swagger
Get Darkaonline/l5-swagger
Composer require "Darkaonline/l5-swagger"
Registering Components
Add \l5swagger\l5swaggerserviceprovider::class to providers in config/app.php, create Swagger-ui, etc.
PHP Artisan L5-swagger:publish
creating a swagger configuration file
After the command is executed, a l5-swagger.php configuration file is generated in the Config directory containing the swagger header, token authentication, routing, and so on.
Where the ' generate_always ' configuration can be modified as needed, it indicates whether to refresh swagger each time. If it is off, modify the swagger configuration to manually refresh using the PHP artisan l5-swagger:generate command. Access Test
In the browser access http://127.0.0.1/api/documentation, you can see the swagger interface, but you will be prompted for the missing Api-docs.json file.
PHP files can be created under the app, such as swagger.php, as follows
<?php
/** *
Class Controller
* *
@package app\http\controllers
* *
@SWG \swagger (
* basepath= "",
* host= "127.0.0.1",
* schemes={"http"},
* @SWG \info (
* version= "1.0",
* title= "Openapi",
* @SWG \contact (name= "Pek Ratanak", url= "https://" Www.google.com "),
* ),
* @SWG \definition (
* definition=" Error ",
* required={"Code", "message"},
* @SWG \property (
* property= "code",
* type= "integer ",
* format=" Int32 "
* ),
* @SWG \property (
* property=" message ",
* type= "String" *) * *)
*
/
Refresh swagger, re-access
PHP Artisan L5-swagger:generate
To add a test API
For example, to create a test TestController
PHP Artisan Make:controller TestController
The contents are as follows
<?php namespace App\http\controllers;
Use Illuminate\http\request; Class TestController extends Controller {/** * @SWG \get (* path= "/api/test", * description= "return
Test Content ", * operationid=" Api.dashboard.index ", * produces={" Application/json "}, * tags={" test "}, * @SWG \parameter (* in= "FormData", * name= "Reason", * type= "string", * Description= "Reasons for taking data", * required=true, *), * @SWG \response (* Response
=200, * description= "Dashboard Overview." *), * @SWG \response (* response=401, * description= "unauthorized action.", *
*)/Public Function index (Request $request) {return response ()->json ( ' Result ' => [' Statistics ' => [' Users ' => [' name ' => ' NamE ', ' email ' => ' user@example.com ']],
' Message ' => ', ' type ' => ' success ', ' status ' => 0]); }
}
Add Lower Route
Route::get (' Test ', ' Testcontroller@index ');
The final effect is as follows
Reference
Integrate Swagger API documentation with Laravel Project (L5 swagger)