25

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
3
  • Is TestClass inside the src directory? Commented Dec 23, 2017 at 17:46
  • No, it's not inside the source directory and that's exactly what I want to accomplish here. I want to put these classes outside of the src folder. Commented Dec 23, 2017 at 17:57
  • 1
    Need to adjust the psr4 section of composer.json and then run composer dump-autoload Commented Dec 23, 2017 at 17:59

2 Answers 2

55

Update your composer.json autoload setup

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

After run: composer dump-autoload and try again.

Sign up to request clarification or add additional context in comments.

4 Comments

composer dump-autoload did the trick for me. Apparently bin/console server:run does not trigger it, and it has to be executed manually.
The problem for me was not executing the composer dump-autoload command. Thanks!
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.
I have to mention that the trick with composer dump-autoload also fixes bugs even in the existing namespace App/. So if you have such an error try to run a command. Me helped.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.