This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

First, access throttling restrictions

Add the restriction code to routes\api.php to restrict access throttling:


      

$api = app('Dingo\Api\Routing\Router');

$api->version('v1'['middleware'= >'api.throttle'.'limit'= >3.'expires'= >1,].function ($api) {
    $api->get('index', [\App\Http\Controllers\TestController::class, 'index']);

    // Name the route
    $api->get('nickname'['as'= >'test.nickname'.'uses'= >'\App\Http\Controllers\TestController@nickname']);

    // Perform login
    $api->post('login', [\App\Http\Controllers\TestController::class, 'login']);

    // The route to log in
    $api->group(['middleware'= >'api.auth'].function($api) {
        $api->get('users', [\App\Http\Controllers\TestController::class, 'users']);
    });

});

Copy the code

And then we usepostmanClick 4 times, the first 3 times are normal, the last time:

Second, internal invocation

The main purpose of this package is to perform requests within the API. It allows you to build your application on top of available apis. Internal requests can also return raw data instead of the original response object, which means you get all the syntactic sugar associated with it. —— reference source dingo/ API internal call

2.1 Build a dispenser instance to initiate internal requests

1. Common request

Build on routes\api.php:

 // Internal call
 $api->get('in', [\App\Http\Controllers\TestController::class, 'in']);
Copy the code

Write the in method under TestController:

public function in() {
        // Build the dispenser instance
        $dispatcher = app('Dingo\Api\Dispatcher');
        // Make an internal request
        $users = $dispatcher->get('api/index');
        return $users;
    }
Copy the code

Effect:

2. Simulate authentication user request

If your API endpoint requires authentication, you can simulate a given user internally. For example, if a user logs in to the application using Laravel’s authentication, you can retrieve the user and then simulate the user making an internal request.

The document herebeThe method is the user instance, and there must be transmission when the user requests ittokenSo let’s take a user directly from the database:

        // Simulate the user
        $user = User::find(1);
        $users = $dispatcher->be($user)->get('api/users');
        return $users;
Copy the code

Effect:

Request the API version

Command line tools and API documentation

4.1 Viewing API Routes

4.2 Generating API Documents

Example of generating API document links:

    /** * log in to the user using 'username' and 'password'. * *@Post("/login")
     * @Versions({"v1"})
     * @Transaction({*@Request({"username": "foo", "password": "bar"}),
     *      @Response(200, body={"id": 10, "username": "foo"}),
     *      @Response(422, body={"error": {"username": {"Username is already taken."}}})
     * })
     */
Copy the code

Run the generate document command line command:php artisan api:docs --output-file /home/vagrant/code/shopApi/document.md Effect (you can see the extra onedocument.mdFile) :But this view has its own limitations, we can generally use the document image on the lineshowDocs. But also be aware of this command-generated MD documentation.

5. Common status codes

2xx: The request is successful, indicating that the request status code is successfully processed.

3xx: The request has been redirected, indicating that further action is required to complete the request. Usually these status codes are used for redirection.

4XX: Request error. These status codes indicate that the request may have failed, preventing the server from processing it.

5XX: Server error. These status codes indicate that the server is experiencing an internal error while trying to process the request. These errors may be the server’s own error, rather than the request’s error.

If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write