Skip to content

Autowire error with Symfony Flex when configuring argument by name #23308

Closed
@ByScripts

Description

@ByScripts
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3.2

There seems to be a problem with autowiring when using Symfony Flex, but not when using a standard symfony new project.

When configuring an argument by name (arguments: { $argName: 'value' }), other arguments are no longer autowired.

At the beginning, I had this service, which was working fine (autowiring works as expected):

namespace App\Service;

class FirstClass
{
  public function __construct(SecondClass $secondClass)
  {
        // ...
  }
}

But then I needed to add a second argument, a plain string. So I did it like this:

public function __construct(SecondClass $secondClass, string $myString)

Then I updated container.yaml accordingly:

services:
  App\Service\FirstClass:
    arguments:
      $myString: 'Some string'

But now I have this error:

Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: Invalid constructor argument 2 for service "App\Service\FirstClass": argument 1 must be defined before. Check your service definition.

If I move the string argument to first position, then I have this error:

Type error: Too few arguments to function App\Service\FirstClass::__construct(), 1 passed in /.../srcDevDebugProjectContainer.php on line 184 and exactly 2 expected in /.../src/Service/FirstClass.php

Here is two demo project, configured the same way. One with Symfony Flex, other as a standard symfony new project:

Flex: https://github.com/ByScripts/sf-issue-autowire-flex
Standard: https://github.com/ByScripts/sf-issue-autowire

Just git clone, composer install, then run bin/console on both project.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions