Dingo\Api\Http\RateLimit\Handler::setThrottle PHP Method

setThrottle() public method

Set the throttle to use for rate limiting.
public setThrottle ( string | Dingo\Api\Contract\Http\RateLimit\Throttle $throttle ) : void
$throttle string | Dingo\Api\Contract\Http\RateLimit\Throttle
return void
    public function setThrottle($throttle)
    {
        if (is_string($throttle)) {
            $throttle = $this->container->make($throttle);
        }
        $this->throttle = $throttle;
    }

Usage Example

Beispiel #1
0
 /**
  * Perform rate limiting before a request is executed.
  *
  * @param \Dingo\Api\Http\Request $request
  * @param \Closure                $next
  *
  * @throws \Symfony\Component\HttpKernel\Exception\HttpException
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $route = $this->router->getCurrentRoute();
     if ($route->hasThrottle()) {
         $this->handler->setThrottle($route->getThrottle());
     }
     $this->handler->rateLimitRequest($request, $route->getRateLimit(), $route->getRateExpiration());
     if ($this->handler->exceededRateLimit()) {
         throw new HttpException(403, 'You have exceeded your rate limit.', null, $this->getHeaders());
     }
     $response = $next($request);
     if ($this->handler->requestWasRateLimited()) {
         return $this->responseWithHeaders($response);
     }
     return $response;
 }