Learning PHP framework (2): laravel


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

LaravelIs different fromThinkPHPThe default routing rule of,LaravelController 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


  • 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 directlyopenapiNot the full path

#Download warehouse code directly

#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.

Convert code annotation generation to metadata

#Usage of OpenAPI
.\vendor\bin\openapi --help

Usage: openapi [--option value] [/path/to/project ...]

  --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


  • Download project

git clone https://github.com/swagger-api/swagger-ui.git

  • Main documents

All you need to do isdistThis folder can be deployed separately or placed in thepublicIn order to avoid readingmetadataCross domain issues, willmetadataSave in the same location

Learning PHP framework (2): laravel

Convert local metadata to API documents

#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

#Convert local metadata to API documents

Learning PHP framework (2): laravel

The position of the above line is determined according to the inputmetadatafilerouteWhen generating API documents, multiple projects are generated according to themetadatafilerouteSwitch API document

  • Format validation

Swagger-UIBy default, themetadataPass on toswagger.ioFormat 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: [
    plugins: [
    layout: "StandaloneLayout"
  • Default metadata read path

In the code aboveurl: "openapi.yaml"It was originallyhttps://petstore.swagger.io/v2/swagger.jsonOfficial 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

#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


  • Download project


  • 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