3

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?

fivejeez
  • 63
  • 1
  • 6
  • Not sure where the error is coming from but you can use the getSubscribedService method in your base controller to easily inject the logger without any other configuration. [Example](https://stackoverflow.com/questions/54590981/symfony-4-how-to-access-the-service-from-controller-without-dependency-injectio/54621546#54621546). It is basically how the AbstractController gets access to it's services. Kind of a shame they didn't include the logger by default. – Cerad Feb 27 '19 at 21:20
  • Thanks, this is definitely useful info. I may try this to get rid of using bind options altogether. As you can see there are still problems with binding params – fivejeez Feb 27 '19 at 21:58
  • You will get those sort of errors if you declare a bind variable but don't use it. And your resource and exclude paths look very strange. Nor should need the instanceof stuff. – Cerad Feb 27 '19 at 22:28
  • I've actually decided to remove the bind and instanceof and set these manually/individually, but now it complains about another error. Also, the resource and exclude paths come from here: https://symfony.com/doc/3.4/service_container.html#creating-configuring-services-in-the-container. The difference is I'm using a whitelist of subpackages instead of a blacklist, and the paths are longer since my service.yml is not under app/config/Resources, but within the bundle src tree. – fivejeez Feb 28 '19 at 17:34
  • I had similar issue, solved by binding on interface instead of argument name : `Psr\Log\LoggerInterface: '@monolog.logger.custom'` – nicolas Mar 27 '19 at 08:16

0 Answers0