Time redirection in cakePHP ?

落爺英雄遲暮 提交于 2019-12-02 19:30:54

问题


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

we can use this if we want to redirect our page in 5 second ,

is there any way to redirect page in 5 second in cakephp ?

if yes please let me know


回答1:


You could try with AppController header() method:

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

In your controller:

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



回答2:


/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);
    }
...

Which shows that the Controller::header( ) function is a simple wrapper for direct calls to the php function header( ).

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

So - to accomplish what you want to do:

/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 )."'" );
        }
        ...
    }
?>

I pass the url to the view and don't die( ) or exit( ) in case you actually wish to render a view. An example:

/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>



回答3:


Do not use $this->header in controller as it will be removed in 3.0. Use CakeResponse::header().

here is working example for cakephp 2.8

Controller:

$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'));

View:

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


来源:https://stackoverflow.com/questions/7258796/time-redirection-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!