【问题标题】:symfony redirects from page to page every couple of secondssymfony 每隔几秒钟从一个页面重定向到另一个页面
【发布时间】:2016-11-28 09:39:57
【问题描述】:

我想从页面索引重定向到幻灯片 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 %}

【问题讨论】:

标签: php symfony redirect twig


【解决方案1】:

首先你不应该从你的视图中刷新页面,所以你需要删除&lt;meta http-equiv="refresh" content="5"&gt;。

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

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

【讨论】:

  • 是的,我按照你评论的那样做了,而且效果很好。 Tnx!
猜你喜欢
  • 2012-08-11
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2014-02-17
  • 1970-01-01
  • 2011-06-02
相关资源
最近更新 更多