-2

I created a simple custom Symfony bundle (in version 5.0). It's after I ran composer require located in vendor/ntrx/ntrx-user-bundle, but I'm not able to load it normally. The folder structure there is like this:

Controller/
Service/
composer.json
NtrxUserBundle.php
Readme.md

The composer.json contains the following:

{
    "name": "ntrx/ntrx-user-bundle",
    "description": "",
    "license": "proprietary",
    "type": "symfony-bundle",
    "require": {
        "php": "^7.2.5",
        [...]
    },
    "autoload": {
        "psr-4": { "Ntrx\\UserBundle\\": "" }
    }
}

And the NtrxUserBundle.php contains the following code:

<?php

namespace Ntrx\UserBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class NtrxUserBundle extends Bundle
{
    /**
     * {@inheritdoc}
     */
    public function build(ContainerBuilder $container): void {
        parent::build($container);
    }
}

When I ran for example php bin/console I get the following error:

Symfony\Component\ErrorHandler\Error\ClassNotFoundError^ {#31
  #message: """
    Attempted to load class "NtrxUserBundle" from namespace "Ntrx\UserBundle".\n
    Did you forget a "use" statement for another namespace?
    """
  #code: 0
  #file: "./src/Kernel.php"
  #line: 23
  trace: {
    ./src/Kernel.php:23 {
      App\Kernel->registerBundles(): iterable^
      › if ($envs[$this->environment] ?? $envs['all'] ?? false) {
      ›     yield new $class();
      › }
    }
    ./vendor/symfony/http-kernel/Kernel.php:369 { …}
    ./vendor/symfony/http-kernel/Kernel.php:123 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:169 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:75 { …}
    ./vendor/symfony/console/Application.php:140 { …}
    ./bin/console:42 { …}
  }
}

I tried to change the names of the bundle or to change the autoloader, but it seems that the class isn't there at all. I also tried to break an other third-party bundle (make a typo in the class name) and I get an corresponding error there but not the error above (The file was found but the class was not in it, the class name or namespace probably has a typo.). Also composer dump-autoload doesn't change anything.

The only similar error I found in the web is Symfony enable custom bundle ClassNotFoundException and I think that there's everything correkt in my code. Any suggestions?

2
  • Do you have this problem in isolation or only when requiring the bundle in a symfony project? Commented Mar 11, 2020 at 13:08
  • 1
    Did your Symfony project's composer.json file get updated with the ntrx dependency? Might check the generated autoload files to see if there is an ntrx entry. Commented Mar 11, 2020 at 13:24

2 Answers 2

0

Even though its an pretty old question, maybe this helps someone:

In case you changed or added namespaces you should run:

composer dump-autoload

After that, Symfony is able to find your classes again.

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

Comments

-1

Thanks to @Cerad for the hint to check the autoload files of composer. There in the autoload_psr4.php the autoload folder was defined in vendor/ntrx/ntrx-user-bundle/src. But the class is in vendor/ntrx/ntrx-user-bundle. The additional subfolder src was part of a new change of the bundle.

I completly removed the vendor folder, executed composer clear-cache and installed all packages with composer. Than it works!

I missed to clear the cache of composer after this change of the autoloader.

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.