【问题标题】:Symfony2 Exception Thrown During Rendering渲染期间抛出 Symfony2 异常
【发布时间】:2014-01-13 17:09:18
【问题描述】:

大家!我一直在关注这个教程:tutorial.symblog.co.uk 在 Symfony2 上一段时间了......唯一的问题是这个教程很老,并且包含许多已弃用甚至删除的功能,我必须自己修复。

无论如何,我猜它“塑造性格”。 ;-)

我最近遇到了另一个我无法解决的问题,所以,我在想,“为什么不问问堆栈中的这些了不起的人呢?”所以,我是……

我正在尝试使用 twig 渲染模板,但它会引发以下错误:

An exception has been thrown during the rendering of a template ("No route found for "GET Comment:create"") in BloggerBlogBundle:Blog:show.html.twig at line 26.
500 Internal Server Error -

这是树枝文件:

{# src/Blogger/BlogBundle/Resources/views/Blog/show.html.twig #}
{% extends 'BloggerBlogBundle::layout.html.twig' %}

{% block title %}{{ blog.title }}{% endblock %}

{% block body %}
    <article class="blog">
        <div class="date"><time datetime="{{ blog.created|date('c') }}">{{ blog.created|date('l, F j, Y') }}</time></div>
        <h2>{{ blog.title }}</h2>
    </article><br>
    {% if asset_exists(['images/', blog.image]|join) %}
        <img src="{{ asset(['images/', blog.image]|join) }}" alt="{{ blog.title }} image not found" class="large" />
    {% endif %}
    <div>
        <p>{{ blog.blog }}</p>
    </div>
    <footer>
        <p>Created by: {{ blog.author }}</p>
    </footer>
    <section class="comments" id="comments">
        <section class="previous-comments">
            <h3>Comments</h3>
            {% include 'BloggerBlogBundle:Comment:index.html.twig' with { 'comments': comments } %}
        </section>
        <h3>Add Comment</h3>
        {% render 'BloggerBlogBundle:Comment:create' %}
    </section>
{% endblock %}

还有评论控制器:

<?php
// src/Blogger/BlogBundle/Controller/CommentController.php

namespace Blogger\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Blogger\BlogBundle\Entity\Comment;
use Blogger\BlogBundle\Form\CommentType;

/**
 * Comment controller.
 */
class CommentController extends Controller {
    public function newAction($blog_id) {
        $blog = $this->getBlog($blog_id);

        $comment = new Comment();
        $comment->setBlog($blog);
        $form = $this->createForm(new CommentType(), $comment);

        return $this->render('BloggerBlogBundle:Comment:form.html.twig', array(
            'comment' => $comment,
            'form' => $form->createView()
        ));
    }

    public function createAction($blog_id) {
        $blog = $this->getBlog($blog_id);

        $comment = new Comment();
        $comment->setBlog($blog);
        $request = $this->getRequest();
        $form = $this->createForm(new CommentType(), $comment);
        $form->bind($request);

        if ($form->isValid()) {
            // TODO: Persist the comment entity

            return $this->redirect($this->generateUrl('BloggerBlogBundle_blog_show', array(
                'id' => $comment->getBlog()->getId())) .
                '#comment-' . $comment->getId()
            );
        }

        return $this->render('BloggerBlogBundle:Comment:create.html.twig', array(
            'comment' => $comment,
            'form' => $form->createView()
        ));
    }

    protected funciton getBlog($blog_id) {
        $em = $this->getDoctrine()
                   ->getManager();

        $blog = $em->getRepository('BloggerBlogBundle:Blog')->find($blog_id);

        if (!$blog) {
            throw $this->createNotFoundException('Unable to find Blog post.');
        }

        return $blog;
    }
}

还有,cmets 的观点:

{# src/Blogger/BlogBundle/Resources/views/Comment/create.html.twig #}

{% extends 'BloggerBlogBundle::layout.html.twig' %}

{% block title %}Add Comment{% endblock %}

{% block body %}
    <h1>Add comment for blog post "{{ comment.blog.title }}"</h1>
    {% include 'BloggerBlogBundle:Comment:form.html.twig' with { 'form' : form } %}
{% endblock %}

一如既往,提前致谢!

【问题讨论】:

    标签: php symfony doctrine-orm twig render


    【解决方案1】:

    您在引用的路径周围缺少controller()

    {{ render(controller('BloggerBlogBundle:Comment:create', {'var1': var1, 'var2': var2})) }}
    

    您还必须使用{{ }} 而不是{% %},因为您要输出渲染。 Here's the Symfony2 docs on embedding other controllers in a Twig template.

    如果没有controller() 方法,Symfony 将尝试使用BloggerBlogBundle:Comment:create 作为路由来加载资源,而不是像您尝试那样直接访问控制器方法。

    【讨论】:

    • 如何通过render传入一个变量?
    • 即使我使用{{ render(controller('BloggerBlogBundle:Comment:create'), { 'blog_id': blog.id }) }},它也会显示An exception has been thrown during the rendering of a template ("Controller "Blogger\BlogBundle\Controller\CommentController::createAction()" requires that you provide a value for the "$blog_id" argument (because there is no default value or because there is a non optional argument after this one).") in BloggerBlogBundle:Blog:show.html.twig at line 26.
    • @xanesis4 您在render(controller( 末尾缺少一个额外的)...我编辑了答案以显示变量传递的示例
    • @xanesis4 圣诞快乐 :)