【发布时间】: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