Time redirection in cakePHP ?
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
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
}
}
/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>
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>
精彩评论