This course shows you how to implement dynamic rate limiting, whereby you will be able to change the rate at which people can access your application without changing the code of your application.
- [Instructor] Rate limiting is a useful feature…to limit the number of requests…a user can pass to your application.…However, it can feel many times like an approach…that is a one size fits all.…Not so in Laravel 5.6,…where you can have a dynamic rate limit feature…using a field from your model…to set an upper number of requests.…To do this, we're going to add a virtual attribute…to our user model.…Open up the app, user.php file,…and add a function on line 30.…
Protected function,…get rate limit attribute.…And in this function, we're going to have it…return a rate limit that we'll control.…In this case, we'll have it return the integer two.…Now open our API routes file…at routesapi.php.…We'll wrap the questions and polls API resource routes…in a new middleware.…
On line 15, before route API resources polls and questions,…we'll add route::middleware,…pass in the string auth.basic, comma,…and then pass in another string that'll be throttle,…so this will be our throttle middleware…rate underscore limit.…This is going to be the user's model attribute…
Share this video
Embed this video
Video: Dynamic rate limiting