sanof sanof - 12 days ago 6
PHP Question

symfony redirects from page to page every couple of seconds

I would like to redirect from page index to slide 1 to slide 2 and then to slide 3 every 5 seconds. How can I do that. So far i try this using this documentation: http://symfony.com/doc/3.1/components/http_foundation.html#redirecting-the-user

and help from this question:

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

In controller:

/**
* 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;
}


into my views:

<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 %}

Answer

First of all you should not refresh page from your views, so you need to remove <meta http-equiv="refresh" content="5">.

And then you could do something similar to this:

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