Expected learning objectives
- Several books officially recommended by laravel
- Key documentation
- Project construction
- Learning points
- Source code learning
Basic points
Command line tools
#Create route under default path
php artisan make:controller Controller
#Create route under default path等价与以下命令
php artisan make:controller App\Http\Controllers\Controller
#Create route under default path等价与以下命令
php artisan make:controller 'App\Http\Controllers\Controller'
#Custom namespace
php artisan make:controller 'App\Api\Auth\Controller'
Route settings
Laravel
Is different fromThinkPHP
The default routing rule of,Laravel
Controller routing needs to be defined
To view the route definition:
php artisan route:list
+--------+----------+-----+------+---------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----+------+---------+------------+
| | GET|HEAD | / | | Closure | web |
+--------+----------+-----+------+---------+------------+
Data migration
#Data constructor
php artisan make:seeder DataSeeder
Installation expansion
Swagger-php
- Download project
The OpenAPI provided by swagger PHP can generate metadata in the CLI (command line), which is the meaning of global installation and can be used directlyopenapi
Not the full path
#Download warehouse code directly
https://github.com/swagger-api/swagger-editor.git
#Install using package management tools
composer require zircote/swagger-php
#Specified version
composer require zircote/swagger-php 2.0.13
#Install using package management tools在全局
composer global require zircote/swagger-php
#Install using package management tools
composer install
Metadata, also known as intermediary data and relay data, is the data about data. It mainly describes the information of data properties, which is used to support such functions as indicating storage location, historical data, resource search, file record, etc.
#Usage of OpenAPI
.\vendor\bin\openapi --help
Usage: openapi [--option value] [/path/to/project ...]
Options:
--output (-o) Path to store the generated documentation.
ex: --output openapi.yaml
--exclude (-e) Exclude path(s).
ex: --exclude vendor,library/Zend
--bootstrap (-b) Bootstrap a php file for defining constants, etc.
ex: --bootstrap config/constants.php
--processor Register an additional processor.
--format Force yaml or json.
--debug Show additional error information.
--help (-h) Display this help message.
#Generate the metadata (yaml format) of the specified file to the specified location
vendor\bin\openapi vendor\zircote\swagger-php\Examples\ -o public\swagger-ui\dist\
#Generate the metadata (JSON format) of the specified file to the specified location
vendor\bin\openapi vendor\zircote\swagger-php\Examples\ -o public\swagger-ui\dist\ --format json
Swagger-ui
- Download project
git clone https://github.com/swagger-api/swagger-ui.git
- Main documents
All you need to do isdist
This folder can be deployed separately or placed in thepublic
In order to avoid readingmetadata
Cross domain issues, willmetadata
Save in the same location
#Installing the HTTP server environment
npm install -g http-server
#Enable services and allow cross domain resource sharing
http-server --cors
#The browser opens the swagger UI interface
http://127.0.0.1:8080/ltf/public/swagger-ui/dist/
#Convert local metadata to API documents
http://127.0.0.1:8080/ltf/public/swagger-ui/dist/openapi.yaml
The position of the above line is determined according to the inputmetadata
fileroute
When generating API documents, multiple projects are generated according to themetadata
fileroute
Switch API document
- Format validation
Swagger-UI
By default, themetadata
Pass on toswagger.io
Format verification. Because it is an internal project of the company, the verification function is turned off
#In index.html Add validatorurl: null to turn off validation
const ui = SwaggerUIBundle({
validatorUrl: null,
url: "openapi.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
- Default metadata read path
In the code aboveurl: "openapi.yaml"
It was originallyhttps://petstore.swagger.io/v2/swagger.json
Official examples
- Automatically update documents
#Create a controller
php artisan make:controller SwaggerController
#Add comments and realize scanning function
/**
* @OA\Info(title="My First API", version="0.1")
*/
/**
* @OA\Get(
* path="/api/resource.json",
* @OA\Response(response="200", description="An example resource")
* )
*/
public function scan()
{
$openapi = \OpenApi\scan(__DIR__);
header('Content-Type: application/x-yaml');
return $openapi->toJson();
}
#Register route
Route::get('scan', '[email protected]');
#Visit page
http://www.ltf.com/scan
#Write path to index.html The scanning function is realized in the file
url: "http://www.ltf.com/scan"
#Visit the API document page to get the latest documents
http://www.ltf.com/doc
Swagger-editor
- Download project
https://github.com/swagger-api/swagger-editor.git
- Generating server code
Swagger version 2.0 can generate PHP back-end code, OpenAPI version 3.0.0 does not support it
Related articles
Learning PHP framework (1): ThinkPHP
Learning PHP framework (2): laravel