-2

我创建了一个简单的自定义 Symfony 包(在 5.0 版中)。这是在我跑到 之后composer requirevendor/ntrx/ntrx-user-bundle但我无法正常加载它。那里的文件夹结构是这样的:

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

composer.json包含以下内容:

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

并且NtrxUserBundle.php包含以下代码:

<?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);
    }
}

例如,当我运行时,php bin/console我收到以下错误:

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 { …}
  }
}

我试图更改捆绑包的名称或更改自动加载器,但似乎该类根本不存在。我还尝试破坏其他第三方捆绑包(在类名中打错字),在那里我得到了相应的错误,但没有上面的错误(The file was found but the class was not in it, the class name or namespace probably has a typo.)。也composer dump-autoload改变不了任何东西。

我在网上发现的唯一类似错误是Symfony 启用自定义捆绑 ClassNotFoundException,我认为我的代码中的所有内容都存在错误。有什么建议么?

4

1 回答 1

-1

感谢@Cerad 提示检查作曲家的自动加载文件。在autoload_psr4.phpautoload 文件夹中定义了vendor/ntrx/ntrx-user-bundle/src. 但班级在vendor/ntrx/ntrx-user-bundle. 附加的子文件夹src是捆绑包新更改的一部分。

我完全删除了vendor文件夹,使用 composer 执行composer clear-cache并安装了所有包。比它有效!

更改自动加载器后,我错过了清除作曲家的缓存。

于 2020-03-11T14:07:05.890 回答