Symfony3提交后需要刷新

时间:2016-04-11 12:17:28

标签: forms redirect submit symfony page-refresh

我希望在提交表单之后 不刷新 我的页面。

我使用重定向再次询问实体列表,但 无变化 ,I还是需要刷新。 我对其他表单有同样的问题,但重定向解决了它。

public function handleClient($client)
{
    if (!$client->getNom() || !$this->clientForm->isSubmitted() || !$this->clientForm->isValid())
        return;

    $this->getDoctrine()->getManager()->persist($client);
    $this->getDoctrine()->getManager()->flush();

    // HANDLE REFRESH LIST OF CLIENT EXPECTED
    $repository = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Client');
    $this->listClients = $repository->getAllClientInverse();

    $this->redirect($this->generateUrl('accueil'));
}


public function clientAction(Request $request)
{
    // ACCUEIL
    $repository = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Client');
    $this->listClients = $repository->getAllClientInverse();

    // HANDLE CLIENT CREATION AND REQUEST
    $this->clientLogicHandler();

    $repository = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Client');
    $this->listClients = $repository->getAllClientInverse();

    return $this->render('CommonBundle:Default:index.html.twig',
        array('listClients' => $this->listClients)
        );
    }
}

编辑: 我发现问题是我直接调用视图而无需从Controller重新加载。有没有办法调用控制器以便呈现新资源?

1 个答案:

答案 0 :(得分:0)

实际上,重定向仅适用于动作,而不适用于儿童功能。 我只需要对动作进行级联,并在需要时进行重定向,如:

public function handleClient($client)
{
    if (!$client->getNom() || !$this->clientForm->isSubmitted() || !$this->clientForm->isValid())
        return false;

    $this->getDoctrine()->getManager()->persist($client);
    $this->getDoctrine()->getManager()->flush();

    // HANDLE REFRESH LIST OF CLIENT EXPECTED
    $repository = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Client');
    $this->listClients = $repository->getAllClientInverse();

    $this->redirect($this->generateUrl('accueil'));
    return true;
}


public function clientAction(Request $request)
{
    // ACCUEIL
    $repository = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Client');
    $this->listClients = $repository->getAllClientInverse();

    // HANDLE CLIENT CREATION AND REQUEST
   if ($this->handleClient())
        $this->redirect($this->generateUrl('accueil'));

    $repository = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Client');
    $this->listClients = $repository->getAllClientInverse();

    return $this->render('CommonBundle:Default:index.html.twig',
        array('listClients' => $this->listClients)
        );
    }
}
相关问题