【问题标题】:Symfony 3.4: Did you forget a "use" statement for another namespace?Symfony 3.4:你是否忘记了另一个命名空间的“use”语句?
【发布时间】:2019-03-23 04:29:11
【问题描述】:

我是 Symfony 的新手。我正在学习一门课程,在创建新的 Symfony 项目后,他们使用此控制台创建新包 php bin/console generate:bundle

但是当我在控制台中这样做,然后使用php bin/console server:run,它会抛出一个错误:

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

您是否忘记了另一个命名空间的“使用”语句?
在 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');
    }
}

我的代码树:

请帮忙,谢谢:)

【问题讨论】:

  • 看起来你的 AppKernel.php 第 19 行中有一个未知类。你能展示你的 AppKernel 吗?你有一个叫做 HomeBundle 的包吗?也许你删除它?你的 composer.json 看起来不错
  • 是的,我会的,请稍候,感谢阅读:)
  • 我已经编辑了帖子,请检查:)顺便说一句,这完全是一个新的交响乐项目,所以我认为AppKernel没有任何问题:)
  • 对于未来的问题,请注意您使用的框架称为 Symfony,而不是 Symphony(这是一个不相关的 CMS)。谢谢 :) 我已经编辑了问题和标签。
  • 是的,谢谢你告诉我 :) 下次我会记住的

标签: symfony bundle


【解决方案1】:

好的,我现在明白了,您应该在 composer.json 中更改它,来自:

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

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

像这样你添加到作曲家自动加载器(vendor/autoload.php)你项目的 src/ 中的所有 php 类

在此之后你需要重新创建你的自动加载文件:

composer dump-autoload

【讨论】:

  • 更改此设置后,我是否需要做其他事情,例如composer install 或其他什么?谢谢你帮助我:)
  • 我编辑了我的帖子,您需要通过 composer install 或 dump-autload 重新生成vendor/autoload.php :)
  • 啊我找到了方法,运行composer dump-autoload
  • 如果你运行 composer update 它将再次生成所有资产并运行
猜你喜欢
  • 1970-01-01
  • 2021-09-05
  • 2016-11-07
  • 2019-11-18
  • 2017-02-12
  • 2015-11-07
  • 1970-01-01
  • 2019-10-09
  • 2020-05-24
相关资源
最近更新 更多