AppKernel.php 奇怪的东西
Posted
技术标签:
【中文标题】AppKernel.php 奇怪的东西【英文标题】:AppKernel.php strange thing 【发布时间】:2017-12-08 01:24:39 【问题描述】:我正在尝试在 Symfony 中设置新项目。当我添加自己的 Bundle 时,出现了这个奇怪的东西:
php 致命错误:未捕获 Symfony\Component\Debug\Exception\ClassNotFoundException:试图从命名空间“ContactBoxBundle”加载类“ContactBoxBundle”。 您是否忘记了另一个名称空间的“使用”语句?在 /home/doro/projects/ContactBox/app/AppKernel.php:18 堆栈跟踪:
0 /home/doro/projects/ContactBox/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(450):AppKernel->registerBundles()
1 /home/doro/projects/ContactBox/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(116):Symfony\Component\HttpKernel\Kernel->initializeBundles()
2 /home/doro/projects/ContactBox/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(137): Symfony\Component\HttpKernel\Kernel->boot() H1>
3 /home/doro/projects/ContactBox/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(124): Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands()
4 /home/doro/projects/ContactBox/vendor/symfony/symfony/src/Symfony/Component/Console/Applicati in /home/doro/projects/ContactBox/app/AppKernel.php 第 18 行
我的 AppKernel 是这样的:
<?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 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');
我真的不知道发生了什么,这是我第一次遇到这种情况,我之前在 Symfony 中做过新项目。有什么想法吗?
composer.json
"name": "doro/contactbox",
"license": "proprietary",
"type": "project",
"autoload":
"psr-4":
"": "src/"
,
"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
【问题讨论】:
在ContactBoxBundle/ContactBoxBundle.php中有没有扩展Bundle
的类?
是的,我使用控制台命令生成了这个 Bundle,所以从技术上讲它应该可以工作
可能是自动加载作曲家的问题,尽管这似乎不太可能。将 composer.json 的自动加载部分粘贴到您的问题中。
AppKernel.php weird behavior的可能重复
【参考方案1】:
你的包在文件中的命名空间是什么:ContactBoxBundle/ContactBoxBundle.php
?
参考here,包名AcmeTestBundle
在命名空间namespace Acme\TestBundle
中;那么appKernel就是new Acme\TestBundle\AcmeTestBundle(),
。
也许这就是你的问题。
【讨论】:
我不这么认为...我的命名空间是路径 src/ContactBoxBundle 的 ContactBoxBundle以上是关于AppKernel.php 奇怪的东西的主要内容,如果未能解决你的问题,请参考以下文章
/app/AppKernel.php 中的奏鸣曲\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle'
RuntimeException 无法扫描“app/AppKernel.php”中的类,该类似乎不是文件也不是文件夹
PHP 致命错误:在第 47 行的 /var/www/znata.com/app/AppKernel.php 中找不到类 'Application\Sonata\MediaBundle\Applica