2012-01-26 6 views
8

私はインデックスアクションを持っています。そのプリントは1)新しいエンティティを作成するためのフォームです。 2)すべてのエンティティのリスト:twigテンプレートにフォームコントローラを埋め込む:サブミット後にエンベデッドコントローラでリダイレクトできない

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

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

    return array(
     'entities' => $entities, 
    ); 
} 

小枝:

{% block content %} 
    {% render "MyBundle:Entity:new" %} 
    {% render "MyBundle:Entity:list" %} 
{% endblock %} 

エンティティコントローラ内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(), 
    ); 
} 

リダイレクトは、エラーが動作していません。

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. 

答えて

3

nun幸いにも、これは設計によるものです。組み込みコントローラからリダイレクトすることはできません。回避策として、別のURLに投稿したり、リファラーを保存したり、そこに必要なものを実行したり、結果とともにリダイレクトしたりすることができます。

+0

あなたが別のコントローラに投稿すると、エラーが発生した場合に、あらかじめ設定されたフォームデータで簡単に現在のコントローラに戻ることはできません... Symfonyのデザインが間違っています – jsgoupil