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