我正在尝试创建一个小应用程序,因此我选择了无脂肪框架。我需要根据成功或错误显示一些消息。假设我是否要添加用户,那么如果成功添加了显示消息,表明用户已成功添加,或者如果没有显示错误消息,则表示无法添加用户。我想不明白。这是我的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');
}
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');
}
}
答案 0 :(得分:7)
我的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');
为了呈现消息,我在我的布局https://github.com/ikkez/fabulog/blob/master/app/ui/templates/alert.html中包含了这个模板,该模板调用了一个转储和清除所有消息的函数,因此它们只会显示一次。您还可以在{{@SESSION.flash}}
等模板令牌中使用SESSION,并将其用于模板中的<repeat>
。