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

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

问题描述:

我希望将页索引重定向到幻灯片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://*.com/a后重定向/ 18176467/1507546 – smarber

+0

Tnx,它的工作! – sanof

首先你不应该刷新页面从您的意见,所以你需要删除<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