渲染期间抛出Symfony2异常

时间:2013-12-26 01:22:17

标签: php symfony doctrine-orm twig render

大家好!我一直在关注这个教程:tutorial.symblog.co.uk在Symfony2上一段时间......唯一的问题是该教程非常陈旧,包含许多已弃用的,甚至已删除的功能,我必须自行修复。

无论如何,我猜它“构建角色”。 ; - )

我最近遇到了另一个我无法解决的问题,所以,我心里想,“为什么不在堆叠上询问那些神奇的人?”所以,我... ...

我正在尝试使用树枝渲染模板,并且它会抛出以下错误:

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 %}

和CommentController:

<?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;
    }
}

并且,评论的观点:

{# 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 %}

一如既往,提前谢谢!

1 个答案:

答案 0 :(得分:2)

您错过了您引用的路径周围的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作为路由加载资源,而不是像您尝试的那样直接访问控制器方法。