cakePHP中的时间重定向?

时间:2011-08-31 14:49:43

标签: cakephp cakephp-1.3 cakephp-1.2

header(“refresh:5; url ='pagetoredirect.php'”);

如果我们想在5秒内重定向页面,我们就可以使用它,

有没有办法在cakephp中在5秒内重定向页面?

如果是,请告诉我

3 个答案:

答案 0 :(得分:1)

您可以尝试使用AppController header()方法:

http://api.cakephp.org/class/app-controller#method-AppControllerheader

在您的控制器中:

class CarController{
   public function add(){
      $this->header("") //Implemented on AppController::header
   }
}

答案 1 :(得分:1)

<强> /cake/libs/controller/controller.php

/**
 * Convenience and object wrapper method for header().  Useful when doing tests and
 * asserting that particular headers have been set.
 *
 * @param string $status The header message that is being set.
 * @return void
 * @access public
 */
    function header($status) {
        header($status);
    }
...

这表明Controller :: header()函数是直接调用php函数header()的简单包装器。

http://api.cakephp.org/class/app-controller#method-AppControllerheader

所以 - 要完成你想做的事:

<强> /app/controllers/examples_controller.php

<?php
    class ExamplesController extends AppController
    {
        public $name = "Examples";
        ...
        public function someAction( ){
            ...
            $url = array( 'controller' => 'examples', 'action' => 'someOtherAction' );
            $this->set( 'url', $url );
            $this->header( "refresh:5; url='".Router::url( $url )."'" );
        }
        ...
    }
?>

我将url传递给视图,如果你真的希望渲染视图,请不要死()或退出()。一个例子:

<强> /app/views/examples/some_action.ctp

<p class='notice'>
    <?php echo $this->Html->link( "You are being redirected to ".Router::url( $url )." in 5 seconds. If you do not wish to wait click here.", $url ); ?>
</p>

答案 2 :(得分:0)

请勿在控制器中使用$ this-&gt;标头,因为它将在3.0中删除。使用CakeResponse :: header()。

这是cakephp 2.8的工作示例

控制器:

$url = array('controller' => 'pages', 'action' => 'index');
        $second = '5';
        if (!$sessionData) {
            return $this->redirect($url);
        }
        $this->Session->delete($bookingStr);
        $this->response->header("refresh:$second; url='" . Router::url($url) . "'");
        $this->set(compact('url', 'second'));

查看:

<p class='notice'>
    <?php echo $this->Html->link( "You are being redirected to ".Router::url($url, TRUE)." in ".$second." seconds. If you do not wish to wait click here.", $url ); ?>
</p>


<div class="step-content"> 
    <div class="booking-form">
        <div class="row">
            Thank you. 
        </div>
    </div>
</div>