Unused binding error when trying to bind logger to autowired controller constructor in Symfony 3.4

3.7k Views Asked by At

After upgrading to Symfony 3.4 from 2.8, I am attempting to get rid of warnings about using services from the container. One hang up is my controller all extend from an abstract controller which needs access to the monolog logger. I've decided to use autwiring for my controllers and have added a constructor in the base controller which has a LoggerInterface $logger as the only argument. In attempt to configure this once, I've added the $logger variable with a reference to the logger service under the bind section of services.yml.

However, I keep getting the error: Unused binding "$logger" in service "security.authentication.failure_handler.secured_area.form_login"

I believe this error is supposed to appear only if no services have a constructor argument with that variable name. Now I know that my controllers all have this in the abstract class, as well as being part of some of my other services, so this seems wrong. How can I get rid of this error?

Here is what my services.yml looks like:

services:

  _defaults:
    autowire: true
    autoconfigure: true
    public: false
    bind:
      $logger: "@logger"
      $env: "%sys_env%"
  _instanceof:
    \Twig_Extension:
      tags: ['twig.extension']

  AppBundle\:
    resource: '../../../../../../src/AppBundle/{Controller,Service,Twig}/*'
    exclude: '../../../../../../src/AppBundle/Service/Exception/*'

  # SECURITY ########################################################################
  security.authentication.failure_handler:
    class:  AppBundle\Security\AuthenticationFailureHandler
    autowire: false
    arguments:  ["@http_kernel", "@security.http_utils", {}, "@app.service.security", "@doctrine.orm.entity_manager", "@logger"]
    tags:
      - { name: 'monolog.logger', channel: 'security' }

UPDATE 1:

I noticed that in security.authentication.failure_handler I have a reference to one of my services: app.service.security. I forgot to declare that below, so I added the following to services.yml:

  app.service.security:
    class: AppBundle\Service\SecurityService

That got rid of the logger error, however now I'm seeing an error about the $env string variable:

Unused binding "$env" in service "security.authentication.failure_handler.secured_area.form_login".

I'm concerned that the error message is not the real error, and this is a red herring. The bind options seem a little flaky. Any advice appreciated...

UPDATE 2:

I've decided to get rid of the bind and instanceof config and am setting up the values manually, but now this is the error: Cannot autowire service "app.service.security": argument "$sysInfoService" of method "AppBundle\Service\SecurityService::__construct()" references class "AppBundle\Service\SystemInfoService" but no such service exists. You should maybe alias this class to the existing "app.service.system_info" service.

What's weird is that I believe I'm doing exactly what the error is suggesting to do; I've added aliases for the supposedly autowired service:

  app.service.system_info:
    class: AppBundle\Service\SystemInfoService
  app.service.security:
    class: AppBundle\Service\SecurityService

I do have some services which I manually declare with autowired: false in order to manually set the arguments. That should be ok, I think; you should be able to have autowired and manual wiring coexisting in the service container, right?

0

There are 0 best solutions below