@@ -230,16 +230,69 @@ prevents that number from being higher than 5,000).
230230Rate Limiting in Action
231231-----------------------
232232
233+ Injecting the Rate Limiter Service
234+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235+
236+ After having configured one or more rate limiters, you have two ways of injecting
237+ them in any service or controller:
238+
239+ **(1) Use a specific argument name **
240+
241+ Type-hint your construtor/method argument with ``RateLimiterFactoryInterface `` and name
242+ the argument using this pattern: "rate limiter name in camelCase" + ``Limiter `` suffix.
243+ For example, to inject the ``anonymous_api `` limiter defined earlier, use an
244+ argument named ``$anonymousApiLimiter ``::
245+
246+ // src/Controller/ApiController.php
247+ namespace App\Controller;
248+
249+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
250+ use Symfony\Component\HttpFoundation\Response;
251+ use Symfony\Component\RateLimiter\RateLimiterFactoryInterface;
252+
253+ class ApiController extends AbstractController
254+ {
255+ public function index(RateLimiterFactoryInterface $anonymousApiLimiter): Response
256+ {
257+ // ...
258+ }
259+ }
260+
261+ **(2) Use the ``#[Target]`` attribute **
262+
263+ When :ref: `dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type >`
264+ the ``#[Target] `` attribute helps you select which one to inject. Symfony creates
265+ a target called "rate limiter name" + ``.limiter `` suffix.
266+
267+ For example, to select the ``anonymous_api `` limiter defined earlier, use
268+ ``anonymous_api.limiter `` as the target::
269+
270+ // ...
271+ use Symfony\Component\DependencyInjection\Attribute\Target;
272+
273+ class ApiController extends AbstractController
274+ {
275+ public function index(
276+ #[Target('anonymous_api.limiter')] RateLimiterFactoryInterface $rateLimiter
277+ ): Response
278+ {
279+ // ...
280+ }
281+ }
282+
233283.. versionadded :: 7.3
234284
235285 :class: `Symfony\\ Component\\ RateLimiter\\ RateLimiterFactoryInterface ` was
236286 added and should now be used for autowiring instead of
237287 :class: `Symfony\\ Component\\ RateLimiter\\ RateLimiterFactory `.
238288
239- After having installed and configured the rate limiter, inject it in any service
240- or controller and call the ``consume() `` method to try to consume a given number
241- of tokens. For example, this controller uses the previous rate limiter to control
242- the number of requests to the API::
289+ Using the Rate Limiter Service
290+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291+
292+ After having injected the rate limiter in any service or controller, call the
293+ ``consume() `` method to try to consume a given number of tokens. For example,
294+ this controller uses the previous rate limiter to control the number of requests
295+ to the API::
243296
244297 // src/Controller/ApiController.php
245298 namespace App\Controller;
@@ -252,8 +305,8 @@ the number of requests to the API::
252305
253306 class ApiController extends AbstractController
254307 {
255- // if you're using service autowiring, the variable name must be:
256- // "rate limiter name" (in camelCase) + "Limiter" suffix
308+ // the argument name here is important; read the previous section about
309+ // how to inject a specific rate limiter service
257310 public function index(Request $request, RateLimiterFactoryInterface $anonymousApiLimiter): Response
258311 {
259312 // create a limiter based on a unique identifier of the client
0 commit comments