无法在CodeIgniter中获取flashdata

时间:2014-02-24 12:38:41

标签: codeigniter

这是我的控制器代码

function index() {
    echo $this->session->flashdata('message');
    $this->load->view('categoryView');
}

function delete() {
    $products = $this->item_model->get_category_id($category_id);
    if (count($products)) {
        $message = 'Category Name is used by the product. Please change them to another category!';
    }
    else {
        $category_id = $this->product_category_model->delete($category_id);
        $message = ($category_id) ? 'Category Deleted Successfully' : 'Failed to Delete Category';
    }

    $this->session->set_flashdata('message', $message);
    redirect('category', 'refresh');
}

调用delete函数后,必须设置flashdata并在同一个控制器的index()函数中检索该值,但我不能。

我也试过$ this-> session-> keep_flashdata('message');在重定向到索引功能之前。但仍然没有得到索引函数的任何价值。

同样改变了$ config ['sess_expire_on_close'] = FALSE; to $ config ['sess_expire_on_close'] = TRUE;仍然没有得到结果。

我浪费了更多时间(将近半天)。请任何人帮助检索codeigniter中的闪存数据。

3 个答案:

答案 0 :(得分:0)

有几种可能性:

1-检查您的浏览器cookie。看看它是否允许使用cookie或是否有其他扩展进行干预。

2- Refresh可能不会向浏览器发送新请求(这意味着新的URL)。试试另一个控制器是否有任何改变

3-硬编码set_flashdata(),其硬编码值而不是变量。 set_flashdata("message", "Thank you");

答案 1 :(得分:0)

在编码器中,您无法在控制器中回显任何内容。

您必须在视图文件中设置$this->session->flashdata('message');而不是在控制器

的index()函数中

$this->session->flashdata('message');放在要显示Flash消息的“categoryView”文件中,再试一次。

答案 2 :(得分:0)

你可以尝试:

function set_flashdata_notification($notify_type, $msg, $error_code = null)
{
    $ci =& get_instance();

    $flashdata_data = set_notification_array($notify_type, $msg, $error_code);
    $ci->session->set_flashdata($flashdata_data);
}

function delete() {
$products = $this->item_model->get_category_id($category_id);
if (count($products)) {
    set_flashdata_notification('Category Name is used by the product.','Please change them to another category!');
    redirect('path_to_view/view','refresh');
}
相关问题