AppKernel.php 奇怪的行为

Posted

技术标签:

【中文标题】AppKernel.php 奇怪的行为【英文标题】:AppKernel.php weird behavior 【发布时间】:2017-12-09 06:50:16 【问题描述】:

所以,我正在尝试创建一个新项目,但内核发生了一些事情,我不太了解。 每次我生成新的 Bundle 并尝试创建控制器或任何东西时,都会出现此错误:

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

0 /var/www/ContactBox/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(450):

AppKernel->registerBundles()

1 /var/www/ContactBox/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(116):

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

2 /var/www/ContactBox/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(137):

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

3 /var/www/ContactBox/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(124):

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

4 /var/www/ContactBox/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(90):

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

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

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

<?php

namespace ContactBoxBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ContactBoxBundle extends Bundle


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
    

请帮忙,我正因此而发疯!

【问题讨论】:

奇怪地类似于:***.com/questions/44908210/…同一作者和所有内容。 我已经回答了两个我不知道是否正确,我还没有看到最古老的问题@Cerad 【参考方案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

【讨论】:

很高兴为您提供帮助,我也回答了您最老的问题

以上是关于AppKernel.php 奇怪的行为的主要内容,如果未能解决你的问题,请参考以下文章

/app/AppKernel.php 中的奏鸣曲\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle'

removeFromParent() 奇怪的行为

parseInt() 函数的奇怪行为

QLcdNumber 奇怪的行为

iOS奇怪的委托行为?

AdditionalSafeAreaInsets 的奇怪行为