Symfony 3.4: 您是否忘记了另一个命名空间的"use"语句?

4
我是新手Symfony。我正在跟随一门课程,创建了一个新的Symfony项目后,他们使用这个控制台创建新的bundle php bin/console generate:bundle

但是当我在控制台中执行此操作,然后使用php bin/console server:run时,它会抛出一个错误:

PHP致命错误:未捕获 Symfony\Component\Debug\Exception\ClassNotFoundException:尝试从名称空间“HomeBundle”加载类“HomeBundle”。

您是否忘记了另一个名称空间的“use”语句?
在C:\xampp\htdocs\symfony_CRUD\app\AppKernel.php的第19行

花了很多时间寻找解决方案,几乎只找到了“更改composer.json”解决方案,但它已经修复了Symfony 3.4的composer.json,所以我不知道该怎么做。

这是我的composer.json开头:

{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle"
        },
        "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
    },
    "autoload-dev": {
        "psr-4": { "Tests\\": "tests/" },
        "files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
    },
    "require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^5.0.0",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.6.4",
        "symfony/symfony": "3.4.*",
        "twig/twig": "^1.0||^2.0"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0"
    },
    "scripts": {
        "symfony-scripts": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-install-cmd": [
            "@symfony-scripts"
        ],
        "post-update-cmd": [
            "@symfony-scripts"
        ]
    },
    "config": {
        "platform": {
            "php": "5.5.9"
        },
        "sort-packages": true
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "web",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "3.4-dev"
        }
    }
}

AppKernel.php

<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    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 HomeBundle\HomeBundle(),
        ];

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

    public function getRootDir()
    {
        return __DIR__;
    }

    public function getCacheDir()
    {
        return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
    }

    public function getLogDir()
    {
        return dirname(__DIR__).'/var/logs';
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(function (ContainerBuilder $container) {
            $container->setParameter('container.autowiring.strict_mode', true);
            $container->setParameter('container.dumper.inline_class_loader', true);

            $container->addObjectResource($this);
        });
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

My code tree:

enter image description here

Please help, thanks :)


看起来你的AppKernel.php文件第19行有一个未知的类。 你能展示一下你的AppKernel吗?你有一个叫HomeBundle的bundle吗?也许你已经删除了它?你的composer.json看起来没问题。 - David Courtey
是的,我会,请稍等,谢谢阅读 :) - fudu
我已经编辑了帖子,请查看 :) 顺便说一下,这是全新的交响乐项目,所以我不认为AppKernel有任何问题 :) - fudu
请注意,将来的问题中您使用的框架称为Symfony,而不是无关的CMS Symphony。谢谢 :) 我已经编辑了问题和标签。 - David Oliver
是的,谢谢你告诉我 :) 我会记住下次的。 - fudu
1个回答

7

好的,我现在明白了,你需要在你的composer.json文件中进行更改,从如下内容开始:

"autoload": {
    "psr-4": {
        "AppBundle\\": "src/AppBundle"
    },
   "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},

to

"autoload": {
    "psr-4": {
        "": "src/"
    },
   "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},

像这样,您可以将项目中 src/ 目录下的所有 PHP 类添加到 composer 自动加载器(vendor/autoload.php)中。

完成后,您需要重新生成自动加载文件:

composer dump-autoload

1
我修改完这个之后,还需要做其他的事情吗,比如运行 composer install 或者其他什么吗?谢谢你的帮助 :) - fudu
我编辑了我的帖子,你需要通过composer install或dump-autload重新生成vendor/autoload.php :) - David Courtey
1
啊,我找到了解决方法,运行composer dump-autoload - fudu
如果你运行composer update,它将重新生成所有资产并运行。 - Mutatos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接