Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DI] Autowiring does not work for single method #35983

Closed
Tarasovych opened this issue Mar 5, 2020 · 9 comments
Closed

[DI] Autowiring does not work for single method #35983

Tarasovych opened this issue Mar 5, 2020 · 9 comments

Comments

@Tarasovych
Copy link

Tarasovych commented Mar 5, 2020

Symfony version affected: 5.0.5

Description

Could not resolve argument $authenticationUtils of "App\Controller\SecurityController:login()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?

How to reproduce

  1. Install these dependencies:
    composer show -i:
doctrine/annotations                  v1.8.0             Docblock Annotations Parser
doctrine/cache                        1.10.0             PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.
doctrine/collections                  1.6.4              PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common                       2.12.0             PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, persistence interfaces, proxies, event syste...
doctrine/dbal                         v2.10.1            Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/doctrine-bundle              2.0.7              Symfony DoctrineBundle
doctrine/doctrine-migrations-bundle   2.1.2              Symfony DoctrineMigrationsBundle
doctrine/event-manager                1.1.0              The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector                    1.3.1              Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                 1.3.0              A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                        1.2.0              PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                   2.2.1              PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very ea...
doctrine/orm                          v2.7.1             Object-Relational-Mapper for PHP
doctrine/persistence                  1.3.6              The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/reflection                   v1.1.0             The Doctrine Reflection project is a simple library used by the various Doctrine projects which adds some additional functionality on top of the reflection functionality that comes with PHP. It allo...
easycorp/easy-log-handler             v1.0.9             A handler for Monolog that optimizes log messages to be processed by humans instead of software. Improve your productivity with logs that are easy to understand.
easycorp/easyadmin-bundle             v2.3.5             Admin generator for Symfony applications
jdorn/sql-formatter                   v1.2.17            a PHP SQL highlighting library
monolog/monolog                       2.0.2              Sends your logs to files, sockets, inboxes, databases and various web services
nikic/php-parser                      v4.3.0             A PHP parser written in PHP
ocramius/package-versions             1.5.1              Composer plugin that provides efficient querying for installed package versions (no runtime IO)
ocramius/proxy-manager                2.2.3              A library providing utilities to generate, instantiate and generally operate with Object Proxies
pagerfanta/pagerfanta                 v2.1.3             Pagination for PHP
psr/cache                             1.0.1              Common interface for caching libraries
psr/container                         1.0.0              Common Container Interface (PHP FIG PSR-11)
psr/event-dispatcher                  1.0.0              Standard interfaces for event handling.
psr/log                               1.1.2              Common interface for logging libraries
symfony/asset                         v5.0.4             Symfony Asset Component
symfony/browser-kit                   v5.0.4             Symfony BrowserKit Component
symfony/cache                         v5.0.4             Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts               v2.0.1             Generic abstractions related to caching
symfony/config                        v5.0.5             Symfony Config Component
symfony/console                       v5.0.4             Symfony Console Component
symfony/css-selector                  v5.0.4             Symfony CssSelector Component
symfony/debug-bundle                  v5.0.4             Symfony DebugBundle
symfony/debug-pack                    v1.0.7             A debug pack for Symfony projects
symfony/dependency-injection          v5.0.5             Symfony DependencyInjection Component
symfony/doctrine-bridge               v5.0.4             Symfony Doctrine Bridge
symfony/dom-crawler                   v5.0.4             Symfony DomCrawler Component
symfony/dotenv                        v5.0.4             Registers environment variables from a .env file
symfony/error-handler                 v5.0.5             Symfony ErrorHandler Component
symfony/event-dispatcher              v5.0.5             Symfony EventDispatcher Component
symfony/event-dispatcher-contracts    v2.0.1             Generic abstractions related to dispatching event
symfony/expression-language           v5.0.4             Symfony ExpressionLanguage Component
symfony/filesystem                    v5.0.5             Symfony Filesystem Component
symfony/finder                        v5.0.4             Symfony Finder Component
symfony/flex                          v1.6.2             Composer plugin for Symfony
symfony/form                          v5.0.4             Symfony Form Component
symfony/framework-bundle              v5.0.4             Symfony FrameworkBundle
symfony/http-client                   v5.0.4             Symfony HttpClient component
symfony/http-client-contracts         v2.0.1             Generic abstractions related to HTTP clients
symfony/http-foundation               v5.0.5             Symfony HttpFoundation Component
symfony/http-kernel                   v5.0.5             Symfony HttpKernel Component
symfony/inflector                     v5.0.5             Symfony Inflector Component
symfony/intl                          v5.0.4             A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/maker-bundle                  v1.14.3            Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                          v5.0.5             A library to manipulate MIME messages
symfony/monolog-bridge                v5.0.4             Symfony Monolog Bridge
symfony/monolog-bundle                v3.5.0             Symfony MonologBundle
symfony/options-resolver              v5.0.5             Symfony OptionsResolver Component
symfony/orm-pack                      v1.0.8             A pack for the Doctrine ORM
symfony/phpunit-bridge                v5.0.4             Symfony PHPUnit Bridge
symfony/polyfill-intl-icu             v1.14.0            Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn             v1.14.0            Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-mbstring             v1.14.0            Symfony polyfill for the Mbstring extension
symfony/polyfill-php73                v1.14.0            Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/profiler-pack                 v1.0.4             A pack for the Symfony web profiler
symfony/property-access               v5.0.5             Symfony PropertyAccess Component
symfony/routing                       v5.0.4             Symfony Routing Component
symfony/security-bundle               v5.0.4             Symfony SecurityBundle
symfony/security-core                 v5.0.4             Symfony Security Component - Core Library
symfony/security-csrf                 v5.0.4             Symfony Security Component - CSRF Library
symfony/security-guard                v5.0.4             Symfony Security Component - Guard
symfony/security-http                 v5.0.4             Symfony Security Component - HTTP Integration
symfony/service-contracts             v2.0.1             Generic abstractions related to writing services
symfony/stopwatch                     v5.0.4             Symfony Stopwatch Component
symfony/test-pack                     v1.0.6             A pack for functional and end-to-end testing within a Symfony app
symfony/translation                   v5.0.4             Symfony Translation Component
symfony/translation-contracts         v2.0.1             Generic abstractions related to translation
symfony/twig-bridge                   v5.0.4             Symfony Twig Bridge
symfony/twig-bundle                   v5.0.4             Symfony TwigBundle
symfony/twig-pack                     v1.0.0             A Twig pack for Symfony projects
symfony/validator                     v5.0.4             Symfony Validator Component
symfony/var-dumper                    v5.0.4             Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter                  v5.0.4             A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-profiler-bundle           v5.0.4             Symfony WebProfilerBundle
symfony/yaml                          v5.0.4             Symfony Yaml Component
twig/extra-bundle                     v3.0.3             A Symfony bundle for extra Twig extensions
twig/twig                             v3.0.3             Twig, the flexible, fast, and secure template language for PHP
zendframework/zend-code               3.4.1              Extensions to the PHP Reflection API, static code scanning, and code generation
zendframework/zend-eventmanager       3.2.1              Trigger and listen to events within a PHP application

composer.json:

    "require": {
        "php": "^7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "easycorp/easyadmin-bundle": "^2.3",
        "myclabs/php-enum": "^1.7",
        "symfony/console": "5.0.*",
        "symfony/dotenv": "5.0.*",
        "symfony/flex": "^1.3.1",
        "symfony/form": "5.0.*",
        "symfony/framework-bundle": "5.0.*",
        "symfony/http-client": "5.0.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/orm-pack": "*",
        "symfony/security-bundle": "5.0.*",
        "symfony/twig-pack": "*",
        "symfony/validator": "5.0.*",
        "symfony/yaml": "5.0.*"
    },
    "require-dev": {
        "symfony/debug-pack": "*",
        "symfony/maker-bundle": "^1.0",
        "symfony/profiler-pack": "^1.0",
        "symfony/test-pack": "*"
    }
  1. Create SecurityController:
<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends AbstractController
{
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        $error = $authenticationUtils->getLastAuthenticationError();

        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render(
            'security/login.html.twig',
            [
                'last_username' => $lastUsername,
                'error' => $error
            ]
        );
    }
}
  1. Add route:
# routes/security.yaml
login:
    controller: App\Controller\SecurityController:login
    path: login
    methods: GET|POST
  1. Enable login in firewalls config:
    firewalls:
        # ...
        main:
            anonymous: lazy
            form_login:
                login_path: login
                check_path: login
  1. Keep services.yml by default:
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:

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.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
  1. Navigate to /login route

Possible Solution
Do not break BC between patch/minor versions

Additional context
5.0.4 is not affected

@drjele
Copy link

drjele commented Mar 6, 2020

i have the same exact situation .. to "fix it" i downgraded "symfony/http-kernel" to "5.0.4"

@xabbuh
Copy link
Member

xabbuh commented Mar 6, 2020

Does it mean that the example works with HttpKernel 5.0.4 but fails with 5.0.5?

@Tarasovych
Copy link
Author

@xabbuh yes

@xabbuh
Copy link
Member

xabbuh commented Mar 6, 2020

Could anyone of you create a small example application that allows to reproduce it?

@perice
Copy link

perice commented Mar 6, 2020

Same here, and @drjele's downgrade fix works.

@Tarasovych
Copy link
Author

Tarasovych commented Mar 6, 2020

Reproducable example https://github.com/Tarasovych/symfony-issue-35983

P. S. It works fine using annotation routes instead of yaml routes

@nicolas-grekas
Copy link
Member

Do not break BC between patch/minor versions

🕯️

@stof
Copy link
Member

stof commented Mar 10, 2020

you are using App\Controller\SecurityController:login in your YAML file, not App\Controller\SecurityController::login (that's the difference with annotations, which will generate the second one).
The single-colon notation is part of a BC layer. It looks like something broke some BC layer.

@stof
Copy link
Member

stof commented Mar 10, 2020

@nicolas-grekas the culprit is symfony/http-kernel@0f85f7e

The issue is that some code was removed due to a comment saying it is deprecated in 4.1. but the corresponding had been un-deprecated in 4.x to allow supporting 3.4 LTS and 4.x at the same time in the ecosystem (see #29218).
The code referenced by that comment has a TODO to deprecate this in 5.1: https://github.com/symfony/http-kernel/blob/021d7d54e080405678f2d8c54cb31d0bb03b4520/Controller/ContainerControllerResolver.php#L37-L40

fabpot added a commit that referenced this issue Mar 11, 2020
…ollers (nicolas-grekas)

This PR was merged into the 5.0 branch.

Discussion
----------

[HttpKernel] Fix support for single-colon syntax for controllers

| Q             | A
| ------------- | ---
| Branch?       | master for features / 3.4, 4.4 or 5.0 for bug fixes <!-- see below -->
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #35983
| License       | MIT
| Doc PR        | -

This reverts commit 6bb6473, reversing
changes made to 4cce23d.

As spotted by @stof in #35983 (comment)

Commits
-------

fbea81c Revert "minor #35559 [FrameworkBundle] remove mention of the old Controller class (nicolas-grekas)"
@fabpot fabpot closed this as completed Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants