<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ali A. Dhillon</title>
    <description>The latest articles on Forem by Ali A. Dhillon (@aleedhillon).</description>
    <link>https://forem.com/aleedhillon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F202718%2Fc4c4441a-f30c-4e2e-869d-5d83b0e87d75.jpeg</url>
      <title>Forem: Ali A. Dhillon</title>
      <link>https://forem.com/aleedhillon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aleedhillon"/>
    <language>en</language>
    <item>
      <title>New simple way of creating custom Rate Limiters in Laravel 8</title>
      <dc:creator>Ali A. Dhillon</dc:creator>
      <pubDate>Sun, 04 Oct 2020 19:21:32 +0000</pubDate>
      <link>https://forem.com/aleedhillon/new-simple-way-of-creating-custom-rate-limiters-in-laravel-8-65n</link>
      <guid>https://forem.com/aleedhillon/new-simple-way-of-creating-custom-rate-limiters-in-laravel-8-65n</guid>
      <description>&lt;p&gt;In &lt;strong&gt;Laravel&lt;/strong&gt; we use &lt;code&gt;throttle&lt;/code&gt; middleware to restrict the amount of traffic for a given route or group of routes. The &lt;code&gt;throttle&lt;/code&gt; middleware accepts two parameters that determine the maximum number of requests that can be made in a given number of minutes.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::middleware('throttle:60,1')-&amp;gt;get('/user', function () {
        //
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here 60 is number of requests you can make in 1 minute.&lt;/p&gt;

&lt;p&gt;Now in &lt;strong&gt;Laravel 8&lt;/strong&gt; there is a new way to create custom Rate Limiters.&lt;br&gt;
We can define our custom Rate Limiter in any Service Provider typically it should be in &lt;code&gt;RouteServiceProvider&lt;/code&gt; like so.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rate limiters are defined using the RateLimiter facade's &lt;code&gt;for&lt;/code&gt; method. The &lt;code&gt;for&lt;/code&gt; method accepts a rate limiter name and a Closure that returns the limit configuration that should apply to routes that are assigned this rate limiter:&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('testing', function (Request $request) {
    return Limit::perMinute(1000);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can attach &lt;code&gt;testing&lt;/code&gt; Rate Limiter using it's name with &lt;code&gt;throttle&lt;/code&gt; middleware like &lt;code&gt;throttle:testing&lt;/code&gt; instead of &lt;code&gt;throttle:60,1&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::middleware('throttle:testing')-&amp;gt;get('/user', function(){
    //
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For further reading check out the &lt;a href="https://laravel.com/docs/8.x/routing#rate-limiting"&gt;Laravel 8 Docs Rate Limiting&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>ratelimiting</category>
      <category>throttling</category>
      <category>routing</category>
    </item>
  </channel>
</rss>
