Rate limiting

To prevent abuse, you should consider adding rate limiting to your APIs. For example, you may want to limit the API usage of each user to be, at most, five API calls within a period of one minute. If too many requests are received from a user within the stated period of time, a response with the status code 429 (Too Many Requests) should be returned.

Getting ready

Repeat all the steps from the Creating a REST server recipe's Getting ready and How to do it... sections.

  1. Create a migration for creating a user allowance table with the following command:
     ./yii migrate/create create_user_allowance_table
    
  2. Then, update the just-created migration method, up, with the following code:
    public function up() { $tableOptions = null; if ($this->db->driverName ...

Get Yii2 Application Development Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.