Symfony 无法注册一个包

Symfony is not able to register a bundle

我在 symproject/src/MyAppBundle/src/Bundle

中有一个捆绑包

此文件夹包含 MyAppBundle.php:

    <?php

    namespace MyCompany\Action\Provider\MyAppBundle\Bundle;

    use Symfony\Component\HttpKernel\Bundle\Bundle;

    class MyAppBundle extends Bundle
    {
    }

然后我注册到App/Appkernel.php:

 public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new MyCompany\Action\Provider\MyAppBundle\Bundle\MyAppbundle(),
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();

            if ('dev' === $this->getEnvironment()) {
                $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
                $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
            }
        }

        return $bundles;
    }

执行此操作后网站将完全无法运行。日志说:

PHP Fatal error: Uncaught Error: Class 'MyCompany\Action\Provider\MyAppProvider\Bundle\MyAppBundle' not found in /var/www/html/symproject/app/AppKernel.php:20\nStack trace:\n#0 /var/www/html/symproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(494): AppKernel->registerBundles()\n#1 /var/www/html/symproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(134): Symfony\Component\HttpKernel\Kernel->initializeBundles()\n#2 /var/www/html/symproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(197): Symfony\Component\HttpKernel\Kernel->boot()\n#3 /var/www/html/symproject/web/app.php(19): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#4 {main}\n thrown in /var/www/html/symproject/app/AppKernel.php on line 20

您已将捆绑包 class 放置到

symproject/src/MyAppBundle/src/Bundle

文件夹,但命名空间是

MyCompany\Action\Provider\MyAppBundle\Bundle

应该是命名空间

App\MyAppBundle\Bundle

并移动到文件夹

symproject/src/MyAppBundle/Bundle

或者您应该更改 composer.json 文件的自动加载部分以使用不同的 class 加载。

https://getcomposer.org/doc/01-basic-usage.md#autoloading