24

Problem

I'm trying to setup a custom directory structure for some shared classes in my Symfony project. I want to create a custom folder in the root of my project and I want to use the Symfony auto-load feature to automatically register services from that folder.

So I added a custom services namespace to the services.yaml file:

# src ./config/services.yaml
services:
    ...

    TestNamespace\:
    resource: '../TestNamespace/*'

  ...

And I added an empty class in the custom folder:

# src ./TestNamespace/TestClass.php

namespace TestNamespace;

class TestClass
{

}

When I run the app I get the following error:

(1/2) InvalidArgumentException
Expected to find class "TestNamespace\TestClass" in file 
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php"
while importing services from resource 
"../TestNamespace/*", but it was not found! Check the
namespace prefix used with the resource.

(2/2) FileLoaderLoadException
Expected to find class "TestNamespace\TestClass" in file 
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php" while 
importing services from resource "../TestNamespace/*", but it was not 
found! Check the namespace prefix used with the resource in 
/path/to/ClassLoadErrorDemo/demo/config/services.yaml (which is loaded 
in resource "/path/to/ClassLoadErrorDemo/demo/config/services.yaml").

I double checked the paths, namespace and the class name multiple times and everything seems fine and I don't understand why I still get the error. Controllers in the ./src folder seem to load fine. What am I doing wrong here?

Steps to reproduce

I created a demo repo to isolate the problem.

git clone https://github.com/smoelker/SymfonyClassLoadErrorDemo.git
cd SymfonyClassLoadErrorDemo/demo
composer install
mv TestNamespace/TestClass.php_ TestNamespace/TestClass.php
php bin/console server:start
Sil
  • 1,120
  • 1
  • 9
  • 30

2 Answers2

51

Update your composer.json autoload setup

{
    [...]
    "autoload": {
        "psr-4": {
            "TestNamespace\\": "TestNamespace/",
            "": "src/"
        }
    },
    [...]
}

After run: composer dump-autoload and try again.

albert
  • 4,290
  • 1
  • 21
  • 42
  • 8
    `composer dump-autoload` did the trick for me. Apparently `bin/console server:run` does not trigger it, and it has to be executed manually. – przemo_li Oct 08 '18 at 08:41
  • The problem for me was not executing the `composer dump-autoload` command. Thanks! – Ignacio Sánchez Nov 14 '18 at 00:17
  • Thank you so much! I was about to go crazy. +1 – code_gamer Mar 16 '20 at 16:31
  • I'm trying to add another namespaced file inside the src/ directory but it is failing with the same message. I have autoload set up properly in my composer.json class os it was working, but this was before I installed symfony. So it seems to have something to do with the way symfony registers classes. – garek007 Dec 29 '22 at 00:37
3

composer dump-autoload --classmap-authoritative will only work if your src directory is present at the time you run the command.

This can be an issue with multi-stage Docker builds in particular, when you are normally only copying the composer.json/composer.lock into the build image.

Ryan
  • 4,594
  • 1
  • 32
  • 35