php Codeigniter flashdata - 我是否正确使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6313157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter flashdata - am I using it correctly?
提问by CyberJunkie
I learned about flashdata and decided to use it to display messages based on database interactions.
我了解了 flashdata 并决定使用它来显示基于数据库交互的消息。
For example if no rows are affected I want to display Post id is invalid or does not exist!
例如,如果没有行受到影响,我想显示 Post id is invalid or does not exist!
In my model
在我的模型中
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;
}
}
and in my view
在我看来
if ($this->session->flashdata('result') != ''):
echo $this->session->flashdata('result');
endif;
This seems to work fine but there is no example in the CI documentation how to pass flashdata between MVC. I'm concerned.. Am I doing this correctly?
这似乎工作正常,但 CI 文档中没有如何在 MVC 之间传递 flashdata 的示例。我很担心..我这样做正确吗?
edit: I seem to have left FALSE and TRUE from a prior attempt. I probably won't need that.
编辑:我似乎在之前的尝试中留下了 FALSE 和 TRUE。我可能不需要那个。
回答by Wesley Murch
Session data is available anywhere in your application, at any time. Calling it directly from a view file is proper, and so it setting it in a Controller.
会话数据可随时随地在您的应用程序中使用。直接从视图文件调用它是正确的,因此它将它设置在控制器中。
There's no need at allto pass it as data with $this->load->view()
- it's redundant. Why assign it to the flashdata in the first place in that case?
有没有必要在所有把它作为数据$this->load->view()
-它是多余的。在这种情况下,为什么首先将其分配给 flashdata?
The way you're doing it is correct.
你这样做的方式是正确的。
EDIT: I just saw you are setting it in the Model instead of Controller - which is highly debatable. I would suggest returning a value from your Model call, and setting the message based on it in your Controller instead.
编辑:我刚刚看到您将它设置在模型中而不是控制器中 - 这是非常有争议的。我建议从您的模型调用中返回一个值,并在您的控制器中设置基于它的消息。
回答by Sumith Harshan
$myArr = array('value 1', 'value 1');
$this->session->set_flashdata('myArray', $myArr);
In the view,
在看来,
print_r($this->session->flashdata('myArray'));
回答by Vamsi Krishna B
I just save it in an array and pass it to the view :)
我只是将它保存在一个数组中并将其传递给视图:)
$data['wow_list'] = $this->Wow_model->getWow($uid);
$this->session->set_flashdata('message', 'Done. You have added new task.');
$data['flash_message'] = $this->session->flashdata('message');
$this->load->view('wow/index', $data);
View
看法
<?= $flash_message ?>
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function.
数据通过视图加载函数的第二个参数中的数组或对象从控制器传递到视图。