我了解了 flashdata 并决定使用它来显示基于数据库交互的消息。
例如,如果没有行受到影响,我想显示 Post id is invalid or does not exist!
在我的模型中
function delete_post($post_id)
{
$this->db->where('user_id', $user_id);
$this->db->where('post_id', $post_id);
$this->db->delete('posts');
if ($this->db->affected_rows() == 0)
{
$this->session->set_flashdata('result', 'Post id is invalid or does not exist!');
redirect('/posts/management');
return FALSE;
}
else
{
redirect('/posts/management');
return TRUE;
}
}
在我看来
if ($this->session->flashdata('result') != ''):
echo $this->session->flashdata('result');
endif;
这似乎工作正常,但 CI 文档中没有关于如何在 MVC 之间传递闪存数据的示例。我很担心..我这样做是否正确?
编辑:我似乎在之前的尝试中留下了 FALSE 和 TRUE。我可能不需要那个。
最佳答案
session 数据可随时随地在您的应用程序中使用。直接从 View 文件调用它是正确的,所以它在 Controller 中设置它。
完全没有必要用$this->load->view()
将它作为数据传递——这是多余的。在这种情况下,为什么首先将它分配给闪存数据?
你这样做的方式是正确的。
编辑:我刚刚看到您在模型而不是 Controller 中设置它 - 这是值得商榷的。我建议从您的模型调用中返回一个值,然后在您的 Controller 中设置基于它的消息。
关于php - Codeigniter flashdata - 我是否正确使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6313157/