Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Symfony cannot autowire a constructor argument because no service has that type as its id

Your error

Cannot autowire service "App\Service\Checkout": argument "$gateway" of method "__construct()" references interface "App\Payment\PaymentGatewayInterface" but no such service exists.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The type-hinted class is excluded from service loading, often an entity

For an entity, inject its repository and load the record in the method that needs it, as the Doctrine guide does with ProductRepository and find().

Fix
// before: public function __construct(private Product $product) {}
class InvoiceMailer
{
    public function __construct(private ProductRepository $products) {}

    public function send(int $productId): void
    {
        $product = $this->products->find($productId);
        // ...
    }
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Cannot autowire service ... references class but no such service exists means Symfony 7.4 or 8.1 found no service whose id equals the type-hint. The class is excluded from loading (often an entity), sits outside the resource path or under the wrong namespace, or is an interface without an alias. Run lint:container, then fix that exclusion, namespace or alias.

  • Applies to Symfony 7.4 LTS, Symfony 8.1, PHP 8.4, PHP 8.5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Compile the container without a request. lint:container prints the full message for every broken argument; debug:autowiring then shows whether the container offers the type at all.

Run this first
php bin/console lint:container
php bin/console debug:autowiring TaxCalculator
php bin/console debug:autowiring Gateway

Then find the line you see in this list and open its fix:

  1. 1The type-hinted class is excluded from service loading, often an entityneeds an instance of "..." but this type has been excluded
  2. 2The class lies outside the resource path, or its namespace does not match its folderreferences class "..." but no such service exists, for a class of your own that is not under src/ or whose namespace differs from its path
  3. 3The argument is an interface, and no single service is aliased to itreferences interface "..." but no such service exists, sometimes with Did you create an instantiable class that implements this interface?

What does the error look like?

  • Cannot autowire service "App\Service\Checkout": argument "$gateway" of method "__construct()" references interface "App\Payment\PaymentGatewayInterface" but no such service exists.
  • Cannot autowire service "App\Service\TaxReport": argument "$calculator" of method "__construct()" references class "App\Billing\TaxCalculator" but no such service exists.
  • argument "$product" of method "__construct()" needs an instance of "App\Entity\Product" but this type has been excluded

Cause 1 of 3

The type-hinted class is excluded from service loading, often an entity

You seeneeds an instance of "..." but this type has been excluded

Check

Cannot autowire service with but this type has been excluded means the class carries the container.excluded tag. Symfony's file loader adds that tag to classes matched by an exclude pattern in services.yaml and to classes marked with the #[Exclude] attribute, and the autowiring pass refuses to inject them. Entities usually end up here because they are data objects that Doctrine loads, not services.

Check
grep -n -A4 "resource:" config/services.yaml
grep -rn "#\[Exclude" src/

Fix

For an entity, inject its repository and load the record in the method that needs it, as the Doctrine guide does with ProductRepository and find(). For any other class, take it out of the exclude list or remove the #[Exclude] attribute.

Fix
// before: public function __construct(private Product $product) {}
class InvoiceMailer
{
    public function __construct(private ProductRepository $products) {}

    public function send(int $productId): void
    {
        $product = $this->products->find($productId);
        // ...
    }
}

Verify

The container compiles without errors.

Verify
php bin/console lint:container

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The class lies outside the resource path, or its namespace does not match its folder

You seereferences class "..." but no such service exists, for a class of your own that is not under src/ or whose namespace differs from its path

Check

references class but no such service exists for your own class means the resource block in services.yaml never registered it. The App\ block with resource ../src/ creates one service per class, with the fully qualified class name as id, and builds each expected class name from the namespace prefix plus the file path; a mismatch makes the loader report Check the namespace prefix used with the resource.

Check
grep -E "^(namespace|final class|class)" src/Billing/TaxCalculator.php
grep -n "resource" config/services.yaml

Fix

Make the namespace match the path under the prefix, or add a block for the other folder with its own prefix. The prefix must end with a backslash, or the loader stops with Namespace prefix must end with a "\".

Fix
# config/services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\:
        resource: '../src/'

    Acme\Billing\:
        resource: '../lib/Billing/'

Verify

debug:autowiring lists the class, and the container compiles.

Verify
php bin/console debug:autowiring TaxCalculator
php bin/console lint:container

Confirmed by symfony.com

Did this fix it?

Cause 3 of 3

The argument is an interface, and no single service is aliased to it

You seereferences interface "..." but no such service exists, sometimes with Did you create an instantiable class that implements this interface?

Check

references interface but no such service exists means nothing is aliased to that interface. Autowiring looks for a service whose id equals the type-hint, and Symfony creates the alias by itself only when exactly one loaded service implements the interface. No implementation loaded, or two of them, leaves the interface without an alias.

Check
php bin/console debug:autowiring PaymentGatewayInterface
grep -rn "implements PaymentGatewayInterface" src/

Fix

Alias the interface to the default implementation. For each other implementation, add a named autowiring alias and pick it with #[Target]; since Symfony 8.1, matching a named alias by argument name alone is deprecated.

Fix
# config/services.yaml
services:
    App\Payment\StripeGateway: ~
    App\Payment\PayPalGateway: ~
    App\Payment\PaymentGatewayInterface $paypalGateway: '@App\Payment\PayPalGateway'
    App\Payment\PaymentGatewayInterface: '@App\Payment\StripeGateway'

// where the non-default one is needed
public function __construct(
    #[Target('paypalGateway')] private PaymentGatewayInterface $gateway,
) {}

Verify

debug:autowiring shows the alias, and the container compiles.

Verify
php bin/console debug:autowiring PaymentGatewayInterface
php bin/console lint:container

Confirmed by symfony.com

Did this fix it?

How do I stop it coming back?

  • Add php bin/console lint:container to the CI pipeline before deployment, as the service container guide suggests.
  • Type-hint repositories, never entities, in service constructors.
  • Give every interface one default alias and pick other implementations only with #[Target].

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Spotless Laundry, EZ Estimater.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.