@@ -225,10 +225,63 @@ prevents that number from being higher than 5,000).
225
225
Rate Limiting in Action
226
226
-----------------------
227
227
228
- After having installed and configured the rate limiter, inject it in any service
229
- or controller and call the ``consume() `` method to try to consume a given number
230
- of tokens. For example, this controller uses the previous rate limiter to control
231
- the number of requests to the API::
228
+ Injecting the Rate Limiter Service
229
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230
+
231
+ After having configured one or more rate limiters, you have two ways of injecting
232
+ them in any service or controller:
233
+
234
+ **(1) Use a specific argument name **
235
+
236
+ Type-hint your construtor/method argument with ``RateLimiterFactoryInterface `` and name
237
+ the argument using this pattern: "rate limiter name in camelCase" + ``Limiter `` suffix.
238
+ For example, to inject the ``anonymous_api `` limiter defined earlier, use an
239
+ argument named ``$anonymousApiLimiter ``::
240
+
241
+ // src/Controller/ApiController.php
242
+ namespace App\Controller;
243
+
244
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
245
+ use Symfony\Component\HttpFoundation\Response;
246
+ use Symfony\Component\RateLimiter\RateLimiterFactoryInterface;
247
+
248
+ class ApiController extends AbstractController
249
+ {
250
+ public function index(RateLimiterFactoryInterface $anonymousApiLimiter): Response
251
+ {
252
+ // ...
253
+ }
254
+ }
255
+
256
+ **(2) Use the ``#[Target]`` attribute **
257
+
258
+ When :ref: `dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type >`
259
+ the ``#[Target] `` attribute helps you select which one to inject. Symfony creates
260
+ a target called "rate limiter name" + ``.limiter `` suffix.
261
+
262
+ For example, to select the ``anonymous_api `` limiter defined earlier, use
263
+ ``anonymous_api.limiter `` as the target::
264
+
265
+ // ...
266
+ use Symfony\Component\DependencyInjection\Attribute\Target;
267
+
268
+ class ApiController extends AbstractController
269
+ {
270
+ public function index(
271
+ #[Target('anonymous_api.limiter')] RateLimiterFactoryInterface $rateLimiter
272
+ ): Response
273
+ {
274
+ // ...
275
+ }
276
+ }
277
+
278
+ Using the Rate Limiter Service
279
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280
+
281
+ After having injected the rate limiter in any service or controller, call the
282
+ ``consume() `` method to try to consume a given number of tokens. For example,
283
+ this controller uses the previous rate limiter to control the number of requests
284
+ to the API::
232
285
233
286
// src/Controller/ApiController.php
234
287
namespace App\Controller;
@@ -241,8 +294,8 @@ the number of requests to the API::
241
294
242
295
class ApiController extends AbstractController
243
296
{
244
- // if you're using service autowiring, the variable name must be:
245
- // "rate limiter name" (in camelCase) + "Limiter" suffix
297
+ // the argument name here is important; read the previous section about
298
+ // how to inject a specific rate limiter service
246
299
public function index(Request $request, RateLimiterFactoryInterface $anonymousApiLimiter): Response
247
300
{
248
301
// create a limiter based on a unique identifier of the client
0 commit comments