วิธีใช้ จะใช้แบบ setFlash, getFlash คู่กันไป
ตัวอย่างดังต่อไปนี้
1. สร้างไฟล์ที่ protected/controllers/WebsiteController.php
<?php
class MemberController extends Controller{
public function actionregis(){
$member = new member();
if(!empty($_POST)){
$member->_attributes = $_POST["member"];
if($member->save()){
$this->redirect(array("Regiscom"));
}
}
$this->render("regis", array(
"member" => $member
));
}
public function actionRegiscom(){
$this->render("Regiscom");
}
}
?>
2. สร้างไฟล์ที่ protected/views/website/index.php
<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="flash-notic">
<?php echo Yii::app()->user->getFlash('success')?>
</div>
<?php endif?>
<?php if(Yii::app()->user->hasFlash('error')):?>
<div class="flash-error">
<?php echo Yii::app()->user->getFlash('error')?>
</div>
<?php endif?>
3. รันด้วย http://localhost/yii_infoweb/app/index.php?r=website/ok
4. รันด้วย http://localhost/yii_infoweb/app/index.php?r=website/bad

เอาไว้เป็นแนวทางประยุกต์ยกตัวอย่างเช่น การเซฟลงฐานข้อมูลว่าสำเร็จหรือไม่ ถ้าไม่สำเร็จก็จะขึ้นแจ้งเตือนในหน้าเีดยกวัน

No comments:
Post a Comment