AppKernel.php怪异行为 [英] AppKernel.php weird behavior

查看:78
本文介绍了AppKernel.php怪异行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个新项目,但是内核确实发生了一些事情,我不太了解. 每当我生成新的Bundle并尝试创建Controller或其他任何东西时,都会出现此错误:

So, I am trying to make a new project, but something is happening with kernel, which I don't really understand. Every time when I generate new Bundle and try to create Controllers or anything, this error appears:

PHP致命错误:未被捕获 Symfony \ Component \ Debug \ Exception \ ClassNotFoundException:尝试执行 载入类别"ContactBoxBundle";来自名称空间"ContactBoxBundle".做过 您忘记了使用"另一个命名空间的声明?在 /var/www/ContactBox/app/AppKernel.php:19堆栈跟踪:

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "ContactBoxBundle" from namespace "ContactBoxBundle". Did you forget a "use" statement for another namespace? in /var/www/ContactBox/app/AppKernel.php:19 Stack trace:

AppKernel-> registerBundles()

AppKernel->registerBundles()

Symfony \ Component \ HttpKernel \ Kernel-> initializeBundles()

Symfony\Component\HttpKernel\Kernel->initializeBundles()

Symfony \ Component \ HttpKernel \ Kernel-> boot()

Symfony\Component\HttpKernel\Kernel->boot()

Symfony \ Bundle \ FrameworkBundle \ Console \ Application-> registerCommands()

Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands()

Symfony \ Bundle \ FrameworkBundle \ Console \ Application->广告 /var/www/ContactBox/app/AppKernel.php,第19行

Symfony\Bundle\FrameworkBundle\Console\Application->ad in /var/www/ContactBox/app/AppKernel.php on line 19

我以前在Symfony上进行过项目,但从未发生过.有任何想法吗?我使用控制台命令"bin/console generate:bundle"生成了Bundle.它会生成应有的一切,默认控制器,模板和类,但是由于此错误,我无法对此做任何进一步的处理.有什么想法吗?

I have made project on Symfony before and it have never happen to me. Any ideas? I generated Bundle using console command "bin/console generate:bundle". It generate everything what it supposed to, co default controller, templates and class, but I am not able to do anything with this further, because of this error. Any ideas?

AppKernel.php

AppKernel.php

<?php

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 AppBundle\AppBundle(),
            new ContactBoxBundle\ContactBoxBundle(),
        ];

        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($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

ContacBoxBundle.php

ContacBoxBundle.php

<?php

namespace ContactBoxBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ContactBoxBundle extends Bundle
{
}

composer.json

composer.json

{
    "name": "root/contactbox",
    "license": "proprietary",
    "type": "project",
    "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": "^3.0.2",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.3.10",
        "symfony/symfony": "3.3.*",
        "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": {
        "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": null
    }
}

请帮助,因此我要疯了!

Please help, I'm getting crazy because of that!

推荐答案

尝试从以下位置更改composer.json文件:

Try to change your composer.json file from this:

"psr-4": {
    "AppBundle\\": "src/AppBundle"
 }

对此:

"psr-4": {
    "": "src/"
},

通过这种方式,您可以将所有Bundle加载到src下.

In this way you load all Bundle under src.

赞:

"psr-4": {
    "AppBundle\\": "src/AppBundle",
    "ContactBoxBundle\\": "src/ContactBoxBundle",
}

或者,您可以根据需要指定要加载的每个捆绑软件.

Or you can specify every Bundle to load if you want.

之后,您需要从控制台进行制作:

After that you need to make from console:

composer dump-autoload

这篇关于AppKernel.php怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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