Symfony 3.4:您是否忘记了“使用”功能?另一个命名空间的声明? [英] Symfony 3.4: Did you forget a "use" statement for another namespace?

查看:119
本文介绍了Symfony 3.4:您是否忘记了“使用”功能?另一个命名空间的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Symfony. I'm following a course, and after creating a new Symfony project, they use this console to create new bundle php bin/console generate:bundle

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

But when I do this in the console, and then use php bin/console server:run, it throws an error:


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

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "HomeBundle" from namespace "HomeBundle".

您是否忘记了另一个名称空间的 use语句?

在C:\xampp\htdocs\symfony_CRUD\app\AppKernel.php:19

Did you forget a "use" statement for another namespace?
in C:\xampp\htdocs\symfony_CRUD\app\AppKernel.php:19

经过大量时间的寻找解决方案之后,几乎我才发现 change composer.json解决方案,但是它已经在Symfony 3.4上修复了composer.json,所以我不知道下一步该怎么做。

After a lot of time searching for solution, almost i just found "change composer.json" solution, but it already fixed composer.json at Symfony 3.4 , so i don't know what to do next.

下面是我的composer.json

Here is my composer.json at the beginning

{
    "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

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:

请帮忙,谢谢:)

推荐答案

好,现在我知道了,您应该在composer.json中,从以下位置更改它:

Ok I see now, you should change this in your composer.json, from :

"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类

Like this you add to the composer autoloader (vendor/autoload.php) all php class inside src/ of your project

此后,您需要重新创建自动加载文件:

After this you need to recreate your autoload file:

composer dump-autoload

这篇关于Symfony 3.4:您是否忘记了“使用”功能?另一个命名空间的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆