在树枝模板中嵌入表单控制器:提交后无法在嵌入式控制器中重定向

Ale*_*rov 8 php symfony

我有索引操作,打印1)用于创建新实体的表单; 2)所有实体的清单:

public function indexAction()
{
    $em = $this->getDoctrine()->getEntityManager();

    $entities = $em->getRepository('MyBundle:Entity')->findAll();

    return array(
        'entities' => $entities,
    );
}
Run Code Online (Sandbox Code Playgroud)

枝条:

{% block content %}
    {% render "MyBundle:Entity:new" %}
    {% render "MyBundle:Entity:list" %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

实体控制器中的newAction是标准形式的控制器:

public function newAction()
{
    $entity = new Entity();
    $form = $this->createForm(new EntityType(), $entity);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            // NOT WORK
            return $this->redirect($this->generateUrl('entity_show',
                array('id' => $entity->getId())));
        }
    }
    return array(
        'form' => $form->createView(),
    );
}
Run Code Online (Sandbox Code Playgroud)

实体持久化后重定向不起作用,错误:

An exception has been thrown during the rendering of a template
("Error when rendering "http://example.com/app_dev.php/url/"
(Status code is 302).") in MyBundle:Default:index.html.twig at line 2.
Run Code Online (Sandbox Code Playgroud)

Ond*_*ták 4

不幸的是,这是设计使然。您无法从嵌入式控制器进行重定向。作为解决方法,您可以发布到不同的 URL,保存引荐来源网址,在其中执行您需要的操作并重定向回结果。