symfony每隔几秒从页面重定向到页面

时间:2016-11-28 09:39:58

标签: php symfony redirect twig

我想从页面索引重定向到幻灯片1到幻灯片2,然后每5秒从幻灯片3重定向一次。我怎样才能做到这一点。到目前为止,我使用此文档尝试此操作:http://symfony.com/doc/3.1/components/http_foundation.html#redirecting-the-user

并从这个问题中获得帮助:

How to auto redirect a user in Symfony after a session time out?

在控制器中:

/**
 * Bisdisp slide show preview action
 *
 * @param int $id
 * @Route("/bisdisp/{id}/slideshow/", name="_get_bisdisp_slideshow", requirements={"id" = "\d+"})
 * @Template()
 */
public function slideshowAction($id)
{
    $power_plant = $this->getPowerPlant($id);
    $response = new RedirectResponse($this->generateUrl('_get_bisdisp_slide1', [ 'id' => $id ]));
    // $response->headers->set('Refresh', 5);

    return $response;
}

/**
 * Slide 1 view
 *
 * @param int $id
 * @Route("/bisdisp/{id}/slideshow/slide1/", name="_get_bisdisp_slide1", requirements={"id" = "\d*"})
 * @Template()
 */
public function slide1Action($id)
{
    $power_plant = $this->getPowerPlant($id);
    $response = new RedirectResponse($this->generateUrl('_get_bisdisp_slide2', [ 'id' => $id ]));
    // $response->headers->set('Refresh', 5);

    return $response;
}

/**
 * Slide 2 view
 *
 * @param int $id
 * @Route("/bisdisp/{id}/slideshow/slide2/", name="_get_bisdisp_slide2", requirements={"id" = "\d*"})
 * @Template()
 */
public function slide2Action($id)
{
    $power_plant = $this->getPowerPlant($id);
    $response = new RedirectResponse($this->generateUrl('_get_bisdisp_slide3', [ 'id' => $id ]));
    // $response->headers->set('Refresh', 5);

    return $response;
}

进入我的观点:

<meta http-equiv="refresh" content="5">
{% block content %}
<h3>Slide index</h3>
{% endblock %}

<meta http-equiv="refresh" content="5">
{% block content %}
<h3>Slide 1</h3>
{% endblock %}

<meta http-equiv="refresh" content="5">
{% block content %}
<h3>Slide 2</h3>
{% endblock %}

1 个答案:

答案 0 :(得分:1)

首先,您不应该从视图中刷新页面,因此您需要删除<meta http-equiv="refresh" content="5">

然后你可以做类似的事情:

private function getRedirectLater($url, $seconds=5)
{
    $response = new Response;
    $response->headers->set('Refresh', $seconds.'; url='. $url);

    return $response;
}

/**
 * Bisdisp slide show preview action
 *
 * @param int $id
 * @Route("/bisdisp/{id}/slideshow/", name="_get_bisdisp_slideshow", requirements={"id" = "\d+"})
 * @Template()
 */
public function slideshowAction($id)
{
    $power_plant = $this->getPowerPlant($id);

    return $this->getRedirectLater($this->generateUrl('_get_bisdisp_slide1', [ 'id' => $id ]));
}

/**
 * Slide 1 view
 *
 * @param int $id
 * @Route("/bisdisp/{id}/slideshow/slide1/", name="_get_bisdisp_slide1", requirements={"id" = "\d*"})
 * @Template()
 */
public function slide1Action($id)
{
    $power_plant = $this->getPowerPlant($id);

    return $this->getRedirectLater($this->generateUrl('_get_bisdisp_slide2', [ 'id' => $id ]));
}

/**
 * Slide 2 view
 *
 * @param int $id
 * @Route("/bisdisp/{id}/slideshow/slide2/", name="_get_bisdisp_slide2", requirements={"id" = "\d*"})
 * @Template()
 */
public function slide2Action($id)
{
    $power_plant = $this->getPowerPlant($id);

    return $this->getRedirectLater($this->generateUrl('_get_bisdisp_slide3', [ 'id' => $id ]));
}