Skip to content

Commit 9dc19c3

Browse files
committed
Move Throttle filter to docs only
1 parent 13aa2ab commit 9dc19c3

3 files changed

Lines changed: 54 additions & 68 deletions

File tree

app/Filters/Throttle.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

user_guide_src/source/incoming/filters.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ and you cannot stop script execution. This does allow you to modify the final ou
8080
the final output. This could be used to ensure certain security headers were set the correct way, or to cache
8181
the final output, or even to filter the final output with a bad words filter.
8282

83-
===================
83+
*******************
8484
Configuring Filters
85-
===================
85+
*******************
8686

8787
Once you've created your filters, you need to configure when they get run. This is done in ``app/Config/Filters.php``.
8888
This file contains four properties that allow you to configure exactly when the filters run.
@@ -94,7 +94,7 @@ The ``$aliases`` array is used to associate a simple name with one or more fully
9494
filters to run::
9595

9696
public $aliases = [
97-
'csrf' => \App\Filters\CSRF::class
97+
'csrf' => \CodeIgniter\Filters\CSRF::class
9898
];
9999

100100
Aliases are mandatory and if you try to use a full class name later, the system will throw an error. Defining them
@@ -181,4 +181,4 @@ a list of URI patterns that filter should apply to::
181181
Provided Filters
182182
****************
183183

184-
Three filters are bundled with CodeIgniter4: Honeypot, Security, and Throttler.
184+
Three filters are bundled with CodeIgniter4: Honeypot, Security, and DebugToolbar.

user_guide_src/source/libraries/throttler.rst

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,53 @@ start using it in your application.
4949
The Code
5050
========
5151

52-
You can find this file at **app/Filters/Throttle.php** but the relevant method is reproduced here::
53-
54-
public function before(RequestInterface $request)
55-
{
56-
$throttler = Services::throttler();
57-
58-
// Restrict an IP address to no more
59-
// than 1 request per second across the
60-
// entire site.
61-
if ($throttler->check($request->getIPAddress(), 60, MINUTE) === false)
62-
{
63-
return Services::response()->setStatusCode(429);
64-
}
65-
}
52+
You could make your own Throttler filter, at **app/Filters/Throttle.php**,
53+
along the lines of::
54+
55+
<?php namespace App\Filters;
56+
57+
use CodeIgniter\Filters\FilterInterface;
58+
use CodeIgniter\HTTP\RequestInterface;
59+
use CodeIgniter\HTTP\ResponseInterface;
60+
use Config\Services;
61+
62+
class Throttle implements FilterInterface
63+
{
64+
/**
65+
* This is a demo implementation of using the Throttler class
66+
* to implement rate limiting for your application.
67+
*
68+
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
69+
*
70+
* @return mixed
71+
*/
72+
public function before(RequestInterface $request)
73+
{
74+
$throttler = Services::throttler();
75+
76+
// Restrict an IP address to no more
77+
// than 1 request per second across the
78+
// entire site.
79+
if ($throttler->check($request->getIPAddress(), 60, MINUTE) === false)
80+
{
81+
return Services::response()->setStatusCode(429);
82+
}
83+
}
84+
85+
//--------------------------------------------------------------------
86+
87+
/**
88+
* We don't have anything to do here.
89+
*
90+
* @param RequestInterface|\CodeIgniter\HTTP\IncomingRequest $request
91+
* @param ResponseInterface|\CodeIgniter\HTTP\Response $response
92+
*
93+
* @return mixed
94+
*/
95+
public function after(RequestInterface $request, ResponseInterface $response)
96+
{
97+
}
98+
}
6699
67100
When run, this method first grabs an instance of the throttler. Next it uses the IP address as the bucket name,
68101
and sets things to limit them to one request per second. If the throttler rejects the check, returning false,
@@ -79,8 +112,7 @@ this to incoming requests, you need to edit **/app/Config/Filters.php** and firs
79112
filter::
80113

81114
public $aliases = [
82-
'csrf' => \App\Filters\CSRF::class,
83-
'toolbar' => \App\Filters\DebugToolbar::class,
115+
...
84116
'throttle' => \App\Filters\Throttle::class
85117
];
86118

@@ -92,9 +124,9 @@ Next, we assign it to all POST requests made on the site::
92124

93125
And that's all there is to it. Now all POST requests made on the site will have be rate limited.
94126

95-
===============
127+
***************
96128
Class Reference
97-
===============
129+
***************
98130

99131
.. php:method:: check(string $key, int $capacity, int $seconds[, int $cost = 1])
100132

0 commit comments

Comments
 (0)