JennySwift
4 years ago
182
10
General

How to use Laravel Throttle

Posted 4 years ago by JennySwift

Hi, I am trying to use Laravel Throttle but am having trouble getting it work and wondering if someone could please help me?

I've followed the installation and configuration instructions. But when I try the following example code from the docs (replacing 'foo' with my own path, and using smaller numbers for the throttle parameters), it doesn't seem to work:

use Illuminate\Support\Facades\Route;

Route::get('foo', array('before' => 'throttle:50,30', function () {
    return 'Why herro there!';
}, ));

I have also tried this other example from the docs, and each time I refresh the page, count($throttler) is still 1:

use GrahamCampbell\Throttle\Facades\Throttle;
use Illuminate\Support\Facades\Request;

// let's quickly get the current request object
$request = Request::getFacadeRoot();

// now let's get a throttler object for that request
// we'll use the same config as in the previous example
// note that only the first parameter is "required"
$throttler = Throttle::get($request, 50, 30);

// let's check if we've gone over the limit
var_dump($thottler->check());

// we implement Countable
var_dump(count($thottler));

I'm assuming $thottler is a typo in the example and should be $throttler.

I figure count($throttler) should increment each time I load the page? But I'm not sure how to get it to do that.

Please sign in or create an account to participate in this conversation.