Php 从其他捆绑包嵌入Symfony控制器
这似乎是一个有趣的问题,我不确定我是否遗漏了什么,因为它应该很简单,但事实并非如此 脚本Php 从其他捆绑包嵌入Symfony控制器,php,symfony,twig,Php,Symfony,Twig,这似乎是一个有趣的问题,我不确定我是否遗漏了什么,因为它应该很简单,但事实并非如此 脚本 我有一个名为AcmePageBundle 另一个名为AcmeGalleryBundle 我想将AcmeGalleryBundlebundle使用到AcmePageBundle 代码 AcmeGalleryBundle\Controller\DisplayController.php AcmeGalleryBundle\Resources\views\Display\embed.html.twig {%用于
AcmePageBundle
AcmeGalleryBundle
AcmeGalleryBundle
bundle使用到AcmePageBundle
{%用于画廊中的照片。照片%}
{%endfor%}
AcmePageBundle\Resources\views\Default\index.html.twig
{#…额外的HTML内容#}
画廊
{{render(controller('AcmeGalleryBundle:Display:embed'{
“galleryId”:123
})) }}
{#HTML内容的其余部分}
错误
当试图呈现使用AcmePageBundle:Default:index.html.twig
的页面时,我得到
在第18行的AcmePageBundle:Default:index.html.twig中呈现模板(“类'AcmeGalleryBundle'不存在”)时引发异常
当然,AcmeGalleryBundle
确实存在。我怀疑我遗漏了一个范围问题
故障排除
AcmeGalleryBundle
加载到AppKernel.php
文件中use Acme\GalleryBundle
添加到AcmePageBundle.php
bundle文件的顶部我遗漏了什么明显的东西吗?我的错误是这样的:
$gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle')->find($galleryId);
命令getRepository('AcmeGalleryBundle')
需要包含我试图加载存储库的实体。正确的代码是:
$gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle:Gallery')->find($galleryId);
我错误地认为这是渲染(控制器(…)功能的问题。修复后,一切正常。下次我应该仔细看看我的堆栈跟踪
{# ... extra HTML content #}
<h2>Gallery</h2>
{{ render(controller('AcmeGalleryBundle:Display:embed', {
'galleryId': 123
})) }}
{# rest of the HTML content #}
$gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle')->find($galleryId);
$gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle:Gallery')->find($galleryId);