【问题标题】:Symfony3 Need to refresh after submitSymfony3 提交后需要刷新
【发布时间】:2016-08-01 14:28:27
【问题描述】:

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

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

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重新加载。有没有办法调用控制器来渲染新资源?

【问题讨论】:

  • 我还是有这个问题。我试过 $this->redirectToRoute('accueil', array(), 302/301);没有变化。

标签: forms redirect submit symfony page-refresh


【解决方案1】:

事实上,重定向只适用于动作,而不适用于子函数。 我只需要对动作进行级联,并在需要时进行重定向,例如:

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)
        );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2020-02-06
    • 2013-08-26
    • 2012-04-03
    相关资源
    最近更新 更多