提问者:小点点

应用内核。奇怪的行为


所以,我试图做一个新项目,但是内核发生了一些事情,我真的不明白。每当我生成新的Bundle并尝试创建Controller或其他东西时,都会出现以下错误:

PHP致命错误:未捕获Symfony\Component\Debug\Exception\ClassNotFoundException:试图从命名空间“ContactBoxBundle”加载类“ContactBoxBundle”。您是否忘记了另一个名称空间的“use”语句?在/var/www/ContactBox/app/AppKernel中。php:19堆栈跟踪:

应用内核-

Symfony\Component\HttpKernel\Kernel-

Symfony\Component\HttpKernel\Kernel-

Symfony\Bundle\FrameworkBundle\Console\Application-

Symfony\Bundle\FrameworkBundle\Console\Application-

我以前在Symfony上做过一个项目,但从来没有发生在我身上。有什么想法吗?我使用控制台命令“bin/console generate:Bundle”生成了Bundle。它生成了它应该生成的所有内容,共同默认控制器、模板和类,但由于这个错误,我无法进一步处理这些内容。有什么想法吗?

应用内核。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');
    }
}

undle.php

<?php

namespace ContactBoxBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ContactBoxBundle extends Bundle
{
}

创作者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
    }
}

请帮帮我,我都快疯了!


共1个答案

匿名用户

尝试从这里更改您的composer.json文件:

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

对此:

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

通过这种方式,您可以在src下加载所有Bundle。

这样地:

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

或者,如果需要,可以指定要加载的每个Bundle。

之后,您需要从控制台执行以下操作:

composer dump-autoload