limiting requests to controllers in laravel 1

limiting requests to controllers in laravel

//Controller add

public function __construct()
	{
		$this->middleware([
			'auth',
//About 20 requests per minute to Cotroller and error if there are too many requests
			'throttle:20,1'
        ]);
	}

Here is what the above code is Doing:
1. We’re using the middleware method on the controller to specify that we want to use the throttle middleware.
2. We’re passing the throttle middleware two arguments: 20 and 1. The first argument is the maximum number of requests we want to allow. The second argument is the number of minutes this limit should apply to.
3. We’re also passing the throttle middleware a name. This name will be used to identify the throttle in the application’s log files.

Now, if a user makes more than 20 requests in a minute, they’ll receive a 429 Too Many Requests response.

If you want to customize the error message that’s returned, you can add a message key to the $errorBag property on the Request class.

For example, if you wanted to customize the error message for the create-post request, you could add the following to the App\Http\Requests\CreatePostRequest class:

protected $errorBag = ‘create-post’;

protected $redirectRoute = ‘posts.create’;

protected $dontFlash = [‘password’, ‘password_confirmation’];

Similar Posts