doupingzhi9674 2014-10-22 19:16
浏览 35
已采纳

从其他包中嵌入Symfony控制器

This seems to be an interesting issue, and I'm not sure if I'm missing something because it should be simple, but isn't.

Scenario

  1. I have one bundle named AcmePageBundle
  2. Another bundle named AcmeGalleryBundle
  3. I want to load an embedded view from the controller using the AcmeGalleryBundle bundle into a template file on AcmePageBundle

Code

AcmeGalleryBundle\Controller\DisplayController.php

class DisplayController extends Controller
{
    public function embedAction($galleryId)
    {
        $gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle')->find($galleryId);
        if (!$gallery) {
            throw $this->createNotFoundException('Gallery could not be found');
        }

        return $this->render('AcmeGalleryBundle:Display:embed.html.twig', array(
            'gallery' => $gallery,
        ));
    }
}

AcmeGalleryBundle\Resources\views\Display\embed.html.twig

  <div class="gallery">
  {% for photo in gallery.photos %}
    <a href="{{ photo.webPath }}" rel="shadowbox[gallery];">
      <img src="{{ photo.webPath }}" height="100" width="100">
    </a>
  {% endfor %}
  </div>

AcmePageBundle\Resources\views\Default\index.html.twig

{# ... extra HTML content #}
  <h2>Gallery</h2>
  {{ render(controller('AcmeGalleryBundle:Display:embed', {
      'galleryId': 123
  })) }}
{# rest of the HTML content #}

Errors

When trying to render the page that uses AcmePageBundle:Default:index.html.twig, I get

An exception has been thrown during the rendering of a template ("Class 'AcmeGalleryBundle' does not exist") in AcmePageBundle:Default:index.html.twig at line 18.

but of course, AcmeGalleryBundle does in fact exist. I'm suspecting a scope problem that I've missed somewhere.

Troubleshooting

  1. Ensured that AcmeGalleryBundle is loaded in the AppKernel.php file
  2. Tried adding use Acme\GalleryBundle to the top of the AcmePageBundle.php bundle file.
  3. Found an issue on GitHub relating to the problem with no response from the devs yet. I've added my own comments - this may in fact be a bug.

Am I missing anything obvious?

  • 写回答

1条回答 默认 最新

  • doubi1928 2014-10-22 19:43
    关注

    My mistake was in this line:

    $gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle')->find($galleryId);
    

    The command getRepository('AcmeGalleryBundle') needs to include the entity I'm trying to load the repository for in question. Correct code is:

    $gallery = $this->getDoctrine()->getRepository('AcmeGalleryBundle:Gallery')->find($galleryId);
    

    I mistakenly thought this was an issue with the render(controller(...)) functionality. After that fix, everything works as it should. Next time I should be looking closer at my stack trace!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?