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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners