RN *_*aha 1 php session-variables fat-free-framework
我正在尝试制作一个小应用程序,所以我选择了无脂肪框架.我需要根据成功或错误显示一些消息.假设我是否要添加用户,那么如果成功添加了显示消息,表明用户已成功添加,或者如果没有显示错误消息,则表示无法添加用户.我想不明白.这是我的Userscontroller代码
public function index(){
$user = new User($this->db);
$this->f3->set('users',$user->all());
//there should be a way to decide if its error message or success and after display,
//it shouldn't be displayed again for the same task.
//or may be it should be check in view file, I don't know where is the correct place
// to do it
$this->f3->set('page_head','User List');
$this->f3->set('view','users/list.htm');
Run Code Online (Sandbox Code Playgroud)
}
public function create(){
if($this->f3->exists('POST.create')){
$user = new User($this->db);
$user->add();
//set session here to show in view file after redirect to list page
$this->f3->reroute('/users');
} else{
$this->f3->set('page_head','Create User');
$this->f3->set('view','users/create.htm');
}
}
Run Code Online (Sandbox Code Playgroud)
我的flash消息控制器如下所示:https://github.com/ikkez/f3-flash/blob/master/lib/flash.php
要设置消息我做:
if ($this->resource->updateProperty(array('_id = ?', $params['id']), 'published', true)) {
\Flash::instance()->addMessage('Your post was published. Hurray!', 'success');
} else {
\Flash::instance()->addMessage('This Post ID was not found', 'danger');
}
$f3->reroute('/admin/post');
Run Code Online (Sandbox Code Playgroud)
为了呈现消息,我在我的布局中包含了这个模板https://github.com/ikkez/fabulog/blob/master/app/ui/templates/alert.html,它调用了一个转储和清除所有消息的函数,因此它们将会只显示一次.您也可以在模板令牌中使用SESSION,{{@SESSION.flash}}并<repeat>在模板中使用它.