0

I'm using Symfony 5.3 and the Symfony RateLimiter bundle.

For this to work, a method should be called by:

public function root(Request $request, RateLimiterFactory $authenticationApiLimiter)
{
}

A RateLimiterFactory requires that the variable be called $authenticationApiLimiter to correctly autowire. However, this project uses the snake_case variables standard.

How do I either alias or change this?

Thanks

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
elb98rm
  • 670
  • 6
  • 21
  • 1
    Why do you think that variable should be called exactly like this? What happens when you rename it? – u_mulder Jul 13 '21 at 14:46
  • It complains that it is cannot find $authentication_api_limiter autowired, and points out that $authenticationApiLimiter IS autowired. – elb98rm Jul 13 '21 at 15:11
  • 3
    You can re-bind parameter using another name. Described here https://symfony.com/doc/current/service_container.html#binding-arguments-by-name-or-type – Ilia Yatsenko Jul 13 '21 at 15:32
  • 1
    The [docs](https://symfony.com/doc/current/rate_limiter.html) say: if you're using service autowiring, the variable name must be: "rate limiter name" (in camelCase) + "Limiter" suffix. You might be better off using camecase here and just assigning it to a snake variable. PHP is gradually shifting to the point of having exact argument names that cannot be changed. May as well get used to it. – Cerad Jul 13 '21 at 16:03
  • Train-case? So dashes in variable names? Are you sure? – Stephan Vierkant Jul 14 '21 at 12:46
  • @Cerad - yup: this is my temporary solution; obviously I was looking for a full time solution. – elb98rm Jul 15 '21 at 15:51
  • @IliaYatsenko : I don't understand this documentation. Could you give a relevant example? Much appreciated. – elb98rm Jul 15 '21 at 15:54

1 Answers1

1

I like most of Symfony's design but they tend to get a bit carried away when it comes to automatically naming things. The best full time solution really is to follow the Symfony standard and just live with it. However you can use bind to change the argument name if you really want to.

# config/services.yaml
services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    
    # named alias    
    Symfony\Component\RateLimiter\RateLimiterFactory $anonymous_api: '@limiter.anonymous_api'

class DefaultController extends AbstractController
{
    #[Route('/', name: 'default')]
    public function index(RateLimiterFactory $anonymous_api): Response
    {
        return $this->render('default/index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    }
}

Instead of a named alias you could also just use a bind. I think the alias is bit more specific but they both work.

To figure out the service id, start by creating a new 5.3 project, installing symfony/rate-limiter and then copying the rate_limiter.yaml file from the docs to config/packages.

bin/console debug:container RateLimiterFactory

 Select one of the following services to display its information:
  [0] Symfony\Component\RateLimiter\RateLimiterFactory $anonymousApiLimiter
  [1] Symfony\Component\RateLimiter\RateLimiterFactory $authenticatedApiLimiter
 > 0
Information for Service "limiter.anonymous_api"
===============================================

 ---------------- -------------------------------------------------- 
  Option           Value                                             
 ---------------- -------------------------------------------------- 
  Service ID       limiter.anonymous_api                             
  Class            Symfony\Component\RateLimiter\RateLimiterFactory  
  Tags             -         

The limiter.anonymous_api is the key. You can use the service in the named aliases.

Cerad
  • 48,157
  • 8
  • 90
  • 92