我用应用程序/控制台创建了一个新包。尝试打印一个简单的 hello,以便我可以继续。我自动加载了命名空间,注册了包,创建了一个页面,但 Symfony 检测到一个异常:
Bundle "PageBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() function of your AppKernel.php file?
但我已经这样做了。
日志显示:
[2011-06-08 23:41:56] request.CRITICAL: InvalidArgumentException: Bundle "PageBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() function of your AppKernel.php file? (uncaught exception) at /Applications/MAMP/htdocs/Symfony/app/bootstrap.php.cache line 634
我还清除了缓存的 dev 文件夹。谁能帮我弄清楚出了什么问题。我以前也这样做过,这是我第一次遇到这个问题。与 bootstrap.php.cache 有关
谢谢!感谢所有的帮助。
代码:
public function registerBundles()
{
$bundles = array(
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 Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Webmuch\PageBundle\WebmuchPageBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Symfony\Bundle\WebConfiguratorBundle\SymfonyWebConfiguratorBundle();
}
return $bundles;
}
该包还在探查器中显示为事件包。
最佳答案
看起来引导缓存没有问题(第 634 行指向 Kernel::getBundles()
方法,这是抛出异常的原因),但以防万一,有一个脚本可以重建它:bin\build_bootstrap.php
.缓存存在减少require()
的数量Symfony 需要加载核心 Symfony 类,只要您使用其中一个测试版,就不太可能出现任何实际问题。
听起来这可能是一个命名问题:您的错误提示缺少 PageBundle,但根据您的内核,该包应该被称为 WebmuchPageBundle。您是否在 routing_dev.yml
中正确引用了它? ?一个示例路由配置是:
page:
resource: "@WebmuchPageBundle/Controller/DefaultController.php"
type: annotation
因为您只为开发和测试环境定义了该包,所以您应该使用
routing_dev.yml
而不是 routing.yml
.接下来,检查包类是否正确命名。您应该在包的根目录中有一个文件(例如
src/Webmuch/PageBundle/WebmuchPageBundle.php
),其中包含以下内容:namespace Webmuch\PageBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class WebmuchPageBundle extends Bundle
{
}
哦,显然要检查 Web 服务器的用户是否可以读取您的包目录。我认为这会引发不同的错误,但值得检查!
关于Symfony2 bundle 未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6284042/