php - 无法通过 Composer 加载类

标签 php namespaces fosuserbundle

我尝试通过以下方式更新学说模式:

$ php bin/console doctrine:schema:update --force

我不断得到:
PHP Fatal error:  Uncaught 
Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to 
load class "FOSUserBundle" from namespace "App\FOS\UserBundle".

内核.php:
 <?php

 namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

class Kernel extends BaseKernel
{
use MicroKernelTrait;

const CONFIG_EXTS = '.{php,xml,yaml,yml}';

public function getCacheDir()
{
    return $this->getProjectDir().'/var/cache/'.$this->environment;
}

public function getLogDir()
{
    return $this->getProjectDir().'/var/log';
}

public function registerBundles()
{
    $bundles = array(
        new FOS\UserBundle\FOSUserBundle(),
    );
    $contents = require $this->getProjectDir().'/config/bundles.php';
    foreach ($contents as $class => $envs) {
        if (isset($envs['all']) || isset($envs[$this->environment])) {
            yield new $class();
        }
    }
}

composer.json 文件:
"autoload": {
    "psr-4": {
        "App\\": "src/",
        "FOS\\": "vendor/"
    }
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "test/"
    }

FOSUserBundle.php 命名空间是:
namespace FOS\UserBundle;

并且不工作,但 namaspace 应用程序工作正常。
我试过:
$ composer dump-autoload

我究竟做错了什么?在 FOS 之前,它仍然采用 App 命名空间。

最佳答案

您需要添加一个斜杠。

$bundles = array(
    new \FOS\UserBundle\FOSUserBundle(),
);

关于php - 无法通过 Composer 加载类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49032851/

相关文章:

php - 如何从php返回数据到eclipse?

c# - 每个程序集一个命名空间?

Symfony2 : How to get user Object inside controller when using FOSUserBundle?

ruby - Nokogiri:如何使用命名空间前缀获取节点名称

symfony - 无法 Autowiring 服务 FOSUserBundle,Symfony 3.4

symfony - 在 Symfony FOSUserBundle 注册中集成验证码的最简单方法?

php - 如何通过单个查询检查多个 ID 是否存在

php - 在上传时将图像转换为所有尺寸与通过 php 请求调整大小

php - time() 是否保证具有闰秒意识?

javascript - 在 JavaScript 中解析命名空间