Introduction to laravel access throttling restrictions and internal calls

1, Restrictions on access throttling

In routes \ API Write the restriction code of access throttling in PHP:

<?php

$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']);

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

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

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

});

Next, we use postman to click 4 times. The first 3 times are normal. The last time:

 

2, Internal call

The main purpose of this package is to execute requests within the API. It allows you to build your application based on the available APIs. The internal request can also return the original data instead of the original response object, which means you can get all the syntax sugar related to it------ Reference source: dingo/api internal call

2.1 build a distributor instance to initiate internal requests

1. General request

In routes \ API Build under PHP:

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

Write in method under TestController:

public function in() {
        // Build distributor instance
        $dispatcher = app('Dingo\Api\Dispatcher');
        // Make internal requests
        $users = $dispatcher->get('api/index');
        return $users;
    }

effect:

 

2. Simulate authenticated 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 obtain the user and then simulate the user to initiate an internal request.

 

 

The document be method is a user instance, and a token must be passed when the user requests it. Then we will demonstrate here to directly take a user from the database:

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

effect:

 

3, Request api version

 

 

 

 

4, Command line tools and api documentation

4.1 viewing api routes

 

 

4.2 generating api documents

Generate api document links example:

    /**
     * User login
     *
     * 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."}}})
     * })
     */
=

 

Run the generate document command line command: PHP artist API: Docs -- output file / home / vagrant / code / shopapi / document md

Effect (you can see multiple document.md files):

 

However, this view has its own limitations. We can generally use online documents such as showDocs. However, you should also know the md document directly generated by the command.

 

5, Common status codes

2xx: the request is successful, indicating the status code of successfully processing the request.

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

4xx: request error. These status codes indicate that the request may be wrong, which hinders the processing of the server.

5xx: server error. These status codes indicate that the server has an internal error when trying to process the request. These errors may be the error of the server itself, not the request error.

On the way to learning php, if you think this article is helpful to you, please pay attention to the praise comment three times. Thank you. Your support must be another support for my blog

Keywords: PHP Laravel Back-end mvc

Added by johnsworld on Sat, 18 Dec 2021 08:19:06 +0200