2016-11-28 111 views
3

我希望將頁索引重定向到幻燈片1到幻燈片2,然後每5秒幻燈片3。我怎樣才能做到這一點。到目前爲止,我嘗試這個使用這個文件:http://symfony.com/doc/3.1/components/http_foundation.html#redirecting-the-usersymfony每隔幾秒將頁面重定向到頁面

,並從這個問題有所幫助:

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

在視圖中刪除'HTTP-equiv'和檢查你怎麼能在SF一些時間http://stackoverflow.com/a後重定向/ 18176467/1507546 – smarber

+0

Tnx,它的工作! – sanof

回答

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

是的,我這樣做就像你評論和它的作品。 TNX! – sanof