Category: Laravel

One of the thing the new Rate Limiter will introduce in Laravel 8 is the brain-dead easy way to throttle certain routes. While this will become very handy for almost everybody, sometimes you won’t want to throttle the whole request, but rather, a single piece of it.

For this, we can use a very simple concept of wrapping the object into another that will check the throttling, and call the method of the target object if it’s not throttled. You can perfectly do something like throttling the next object method to call this way: For this to work, we can create a method on a trait that will create an object that uses the Rate Limiter, checks the method to throttle, and then executes the method if the limit has not been set.

We can even chain methods with this throttle: Of course you can expand on this, like using a custom key, and allow to clear the throttle key or reset it’s condition, and finally, use a Closure to execute alternate logic if the action is throttled.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners