Search code examples
phpsymfonydependency-injectionfactory-pattern

Symfony 3 - The definition for (file path) has no class attribute and appears to reference a class or interface in the global namespace


Has anyone encountered the error below and know what the issue could be? I am using an Exception bundle to output JSON-based exception messages for a REST API.

    C:\htdocs\projects\myproject>php bin/console cache:clear --no-warmup
    PHP Fatal error:  Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: The definition
     for "ApiExceptionBundle\Component\Factory\ResponseFactoryInterface" has no class attribute, and appears to
     reference a class or interface in the global namespace. Leaving out the "class" attribute is only allowed
    for namespaced classes. Please specify the class attribute explicitly to get rid of this error. in C:\htdoc
    s\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\CheckDefinitio
    nValidityPass.php:52
    Stack trace:
    #0 C:\htdocs\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\Com
    piler.php(143): Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass->process(Object(
    Symfony\Component\DependencyInjection\ContainerBuilder))
    #1 C:\htdocs\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\ContainerBui
    lder.php(731): Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object(Symfony\ in C:\htdoc
    s\projects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\CheckDefinitio
    nValidityPass.php on line 52

    Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: The definition for
    "ApiExceptionBundle\Component\Factory\ResponseFactoryInterface" has no class attribute, and appears to refe
    rence a class or interface in the global namespace. Leaving out the "class" attribute is only allowed for n
    amespaced classes. Please specify the class attribute explicitly to get rid of this error. in C:\htdocs\pro
    jects\myproject\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\CheckDefinitionVali
    dityPass.php on line 52

    Symfony\Component\DependencyInjection\Exception\RuntimeException: The definition for "ApiExceptionBundle\Co
    mponent\Factory\ResponseFactoryInterface" has no class attribute, and appears to reference a class or inter
    face in the global namespace. Leaving out the "class" attribute is only allowed for namespaced classes. Ple
    ase specify the class attribute explicitly to get rid of this error. in C:\htdocs\projects\myproject\vendor\s
    ymfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass.php on line 5
    2

    Call Stack:
        4.6333   13914032   1. Symfony\Component\Debug\ErrorHandler->handleException() C:\htdocs\projects\alire
    st\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php:0

C:\htdocs\projects\myproject>

Here is the file it is referencing:

<?php

namespace ApiExceptionBundle\Component\Factory;

use ApiExceptionBundle\Component\Api\ApiProblemInterface;

interface ResponseFactoryInterface
{
    public function createResponse(ApiProblemInterface $apiProblem);
}

Solution

  • All is here in the doc

    the type-hint (ApiExceptionBundle\Component\Api\ApiProblemInterface) does not match the id of the service (ApiExceptionBundle\Component\Api\ApiProblem ?). This means that the argument can't be autowired.

    You should add an alias

    services:
        ApiExceptionBundle\Component\Api\ApiProblemInterface: '@ApiExceptionBundle\Component\Api\ApiProblem'