symfony形式:如何在视图中显示参数;变量不存在&”;


symfony form : how to show the parameter in the view, "variable does not exist..."

我的类Article使用了一个包含两个类("ArticleType"answers"ArticleHandler")的表单。

我想发送我刚刚创建的文章的id,但我无法在视图中显示这个id参数:

在控制器中,我发送我的文章的id:

$handler = new ArticleHandler($form, $request, $em);
if ($handler->process()){
    return $this->redirect($this->generateUrl('myproject_show', array('id' => $article->getId())) );
}

在视图中,我有一个错误:

{% block body %}
    <p>the id :</p>
    {{ id }}
{% endblock %}

entity.id(如在CRUD中):

变量"id"不存在
变量"entity.id"不存在。。。

你知道怎么解决这个问题吗?

感谢


编辑:这是我的方法:

public function addAction()
{
    $article = new Article();
    $form = $this->createForm(new ArticleType(), $article);
    $request = $this->getRequest();
    $em = $this->getDoctrine()->getEntityManager();
    $handler = new ArticleHandler($form, $request, $em);
    if ($handler->process()){
        return $this->redirect($this->generateUrl('myproject_show', array('id' => $article->getId())) );
    }
    return $this->render('ProjBlogBundle:Blog:add.html.twig', array(
        'form' => $form->createView(),
    ));
}

下面是视图:

{% extends "ProjBlogBundle::layout.html.twig" %}
{% block title %}
  the title - {{ parent() }}
{% endblock %}
{% block sousbody %}
    <p>here's the article i've just created :</p>
    {{ id }}
{% endblock %}

编辑N°2:

myproject_show:
    pattern:  /show/{id}
    defaults: { _controller: ProjBlogBundle:Blog:show, id:5 }

要在模板中使用变量,您需要在渲染模板时传递它:

//ProjBlogBundle:Blog:show
public function showAction($id)
{
    return $this->render('ProjBlogBundle:Blog:show.html.twig', array(
        'id' => $id
    ));
}

$this->redirect($this->generateUrl('myproject_show', array('id' => $article->getId())) );只返回HTTP 302响应而不呈现模板,浏览器被重定向到生成的url。。。