【问题标题】:How to redirect after create action in SF2 controller如何在 SF2 控制器中创建操作后重定向
【发布时间】:2013-03-24 15:52:54
【问题描述】:

这是无法正常工作的操作,实体已创建,但我总是在渲染 AileronsFrontendBundle:Default:observation.html.twig

如代码所示,这个 createAction 应该渲染我的 home 模板,但是模板渲染不是很好。

 /**
 * Creates a new Observation entity.
 *
 * @Route("/observation/new", name="observation_create")
 * @Method("POST")
 * @Template("AileronsFrontendBundle:Default:observation.html.twig")
 */
public function createAction(Request $request) {
    $entity = new Observation();
    $form = $this->createForm(new ObservationType(), $entity);
    $form->bind($request);


    if ($form->isValid()) {
        $entity->getObservator()->setIp($request->getClientIp());
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        $msg = array(
            'type'=>'success',
            'title'=>'Merci !',
            'text'=>'Votre observation à bien été enregistré, merci pour votre participation !',
        );
        $this->redirect($this->generateUrl('home', array('msg'=>$msg)));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );

我已经尝试过重定向:

$this->render('AileronsFrontendBundle:Default:index.html.twig', array('msg'=>$msg));

它也不起作用。

这是我的索引操作

/**
 * @Route("/", name="home")
 * @Template()
 */
public function indexAction() {
    return array();
}

【问题讨论】:

  • 你有什么问题?
  • 这个 createAction 应该渲染我的 home 模板,但是模板渲染不是很好。我总是在渲染 AileronsFrontendBundle:Default:observation.html.twig

标签: redirect symfony routing routes


【解决方案1】:

您需要返回您的重定向。

return $this->redirect($this->generateUrl('home', array('msg'=>$msg)));

一个动作总是需要返回一个响应对象。 返回渲染的 Twig 文件的示例:

return $this->render('AcmeTestBundle:Test:test.html.twig', array());

【讨论】:

  • 太可惜了!为什么我没有看到!感谢指出这一点
猜你喜欢
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
相关资源
最近更新 更多