Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

symfony - AppKernel.php weird behavior

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 Fatal error: Uncaught SymfonyComponentDebugExceptionClassNotFoundException: 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:

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):

SymfonyComponentHttpKernelKernel->initializeBundles()

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

SymfonyComponentHttpKernelKernel->boot()

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

SymfonyBundleFrameworkBundleConsoleApplication->registerCommands()

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

SymfonyBundleFrameworkBundleConsoleApplication->ad in /var/www/ContactBox/app/AppKernel.php on line 19

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

<?php

use SymfonyComponentHttpKernelKernel;
use SymfonyComponentConfigLoaderLoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new SymfonyBundleFrameworkBundleFrameworkBundle(),
            new SymfonyBundleSecurityBundleSecurityBundle(),
            new SymfonyBundleTwigBundleTwigBundle(),
            new SymfonyBundleMonologBundleMonologBundle(),
            new SymfonyBundleSwiftmailerBundleSwiftmailerBundle(),
            new DoctrineBundleDoctrineBundleDoctrineBundle(),
            new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),
            new AppBundleAppBundle(),
            new ContactBoxBundleContactBoxBundle(),
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new SymfonyBundleDebugBundleDebugBundle();
            $bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle();
            $bundles[] = new SensioBundleDistributionBundleSensioDistributionBundle();

            if ('dev' === $this->getEnvironment()) {
                $bundles[] = new SensioBundleGeneratorBundleSensioGeneratorBundle();
                $bundles[] = new SymfonyBundleWebServerBundleWebServerBundle();
            }
        }

        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 SymfonyComponentHttpKernelBundleBundle;

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
    }
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to change your composer.json file from this:

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

to this:

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

In this way you load all Bundle under src.

Like this:

"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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...