CakePHPのrenderとredirectのメモ
ViewはView/Examples/index.cptのみ。
class Example extends AppController {
public function index() {
$index = 'index : ' . date( 'Y:m:d H:i:s' ); // --- 変数index
$this->set( 'index', $index );
}
public function some() {
$some = 'some : ' . date( 'Y:m:d H:i:s' );
$this->Session->setFlash( 'someメッセージ' );
$this->set( 'some', $some ); // 変数some
$this->render( 'index' );
}
public function other() {
$other = 'other : ' . date( 'Y:m:d H:i:s' );
$this->Session->setFlash('otherメッセージ。' );
$this->set( 'other', $other ); // --- 変数other
$this->redirect( [ 'controller' => 'example', 'action' => 'index' ] );
}
public function complex() {
if ( true ) {
$some = 'some : ' . date( 'Y:m:d H:i:s' );
$this->Session->setFlash( 'someメッセージ' );
$this->set( 'some', $some );
$this->render( 'index' ); // --- complexメソッドの処理は終了しない。
}
$other = 'other : ' . date( 'Y:m:d H:i:s' );
$this->Session->setFlash('otherメッセージ。');
$this->set( 'other', $other ); // --- otherはindexビューで利用できない。
$this->redirect( [ 'controller' => 'example', 'action' => 'index' ] );
}
}
renderメソッドは処理を終了しない。return falseを付けると呼び出し元へ戻る(complexメソッドは終了)。
No comments yet.
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。