简体   繁体   English

Codeigniter中FlashData的奇怪行为

[英]Strange behavior with flashdata in codeigniter

I have found very strange behavior of "if" condition and session flashdata in codeigniter, 我在codeigniter中发现了“ if”条件和会话flashdata的异常行为,

public function edit_equip($equip_id, $company_id) {
        $this->data['section_title'] = 'Edit Equipment';
        $this->data['equip_detail'] = $equip_detail = $this->common->select_data_by_id('equipment', 'id', $equip_id, '*', array());
        if (empty($equip_detail)) {
            $this->session->set_flashdata('error', 'Error Ouccrred. Try Again!');
            redirect('Company/equipment/' . $company_id, 'refresh');
        }
       //some other code
        $this->load->view('company/edit_equip', $this->data);
    }

this is a function of my Equipment class. 这是我的设备类的功能。 Now when I call below url like, http://localhost/scale/Equipment/edit_equip/1/2 现在,当我在下面的网址中调用时,例如http:// localhost / scale / Equipment / edit_equip / 1/2

then edit view will open correctly. 然后编辑视图将正确打开。 but now when I press F5 button or browser refresh button then it is showing "Error Ouccrred. Try Again!" 但是现在当我按F5按钮或浏览器刷新按钮时,它显示“错误提示。请重试!” flash message which I have set in above if condition. 如果条件已在上面设置的即时消息。 I am not understand why this is happening, because $equip_detail contains data and I have also try to die in it but it is not going into if which is correct so why only $this->session->set_flashdata('error', 'Error Ouccrred. Try Again!'); 我不明白为什么会这样,因为$ equip_detail包含数据,而且我也试图在其中消亡,但是如果正确,它将不起作用,所以为什么只有$this->session->set_flashdata('error', 'Error Ouccrred. Try Again!'); is providing effect? 提供效果?

in conclusion my code is not running if block but if i press F5 or browser refresh button after first time my view is loaded it is showing me error message which is set in if condition, but my code is not going to it otherwise it has to redirect but it is loading view page with flash message. 总之,如果阻塞,我的代码不会运行,但是如果我在第一次加载视图后按F5或浏览器刷新按钮,则会向我显示错误消息,该消息设置为if条件,但我的代码不会继续执行,否则必须重定向,但它正在加载带有Flash消息的视图页面。

I have only set this flash message at only one place as per above code. 根据上述代码,我仅在一个位置设置了此即显消息。 I am using codeigniter 3.1.6 我正在使用Codeigniter 3.1.6

Please can anyone explain me this? 请有人能解释一下吗?

the CI manual says: CI手册说:

CodeIgniter supports “flashdata”, or session data that will only be available for the next request , and is then automatically cleared. CodeIgniter支持“ flashdata”或仅对下一个请求可用的会话数据,然后将其自动清除。

F5 is sending a new request, so flashdata is cleared F5正在发送新请求,因此清除了flashdata

This isn't the solution, but more of an investigation to satisfy yourself how things are working... 这不是解决方案,而是更多的调查工作,以使自己满意。

So when things go "screwy", its always a good time to go back to basics. 因此,当事情变得“混乱”时,总是回到基础知识的好时机。 So seeing as you are so convinced your code is executing correctly but giving you unexpected results here is some test code just to check out the behavior of flashdata. 因此,您可以确信自己的代码可以正确执行,但是给您意想不到的结果,这是一些测试代码,目的只是为了检查flashdata的行为。

Flash_session_test.php Flash_session_test.php

class Flash_session_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
    }

    public function index()
    {
        echo "This is " . __METHOD__;
        $this->test_display();
    }

    /**
     * Mockup code derived from sample code
     *  to test the behavior of the flash data.
     *
     * @param $equip_id
     */
    public function edit_equip($equip_id)
    {
        echo "This is " . __METHOD__;

        // Set up our Fail / Pass for the If statement below.
        if (isset($equip_id)) {
            if ($equip_id == 'fail') {
                // Create a Fail Condition
                $equip_detail = null;
            } else {
                // Create a Pass Condition
                $equip_detail = array('fred' => 1);
            }
        }
        // The code we are testing.
        if (empty($equip_detail)) {
            $this->session->set_flashdata('error', 'Error Occurred. Try Again!');
            // Redirect and display the flashdata error message
            redirect('/flash_session_test', 'refresh');
        }
        $this->test_display();
    }

    /**
     * Our Test "View" put here for simplicity
     */
    public function test_display()
    {
        echo '<br>';
        echo '<a href="/flash_session_test/edit_equip/pass" >Click here to Create a PASS</a>';
        echo '<br>';
        echo '<a href="/flash_session_test/edit_equip/fail" >Click here to Create an ERROR</a>';
        echo '<br>';
        echo '<br>';
        // Only display the message if we have one
        $error = $this->session->flashdata('error');
        if ($error === null) { // If null, then It doesn't exist
            echo '<div style="color:green">';
            echo "Not An Error in Sight!";
            echo '</div>';
        } else {
            echo '<div style="color:red">';
            echo $this->session->flashdata('error');
            echo '</div>';
            echo "Now Refresh the page!";
        }
    }
}

Note: This will run standalone, without relying on your existing code. 注意:这将独立运行,而不依赖您现有的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM