【问题标题】:Strange behavior with flashdata in codeignitercodeigniter 中 flashdata 的奇怪行为
【发布时间】:2017-12-07 20:19:15
【问题描述】:

我在 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);
    }

这是我的 Equipment 类的一个函数。现在当我像下面这样调用网址时, http://localhost/scale/Equipment/edit_equip/1/2

然后编辑视图将正确打开。但是现在当我按下 F5 按钮或浏览器刷新按钮时,它会显示“错误 Ouccrred。再试一次!”如果条件,我在上面设置的闪存消息。我不明白为什么会发生这种情况,因为 $equip_detail 包含数据,我也尝试过死在其中,但如果哪个正确,它不会进入,所以为什么只有 $this->session->set_flashdata('error', 'Error Ouccrred. Try Again!'); 有效果吗?

总之,如果阻塞,我的代码没有运行,但是如果我在第一次加载视图后按 F5 或浏览器刷新按钮,它会显示在 if 条件下设置的错误消息,但我的代码不会在其他情况下运行它必须重定向,但它正在加载带有 Flash 消息的视图页面。

按照上面的代码,我只在一个地方设置了这个 flash 消息。我正在使用 codeigniter 3.1.6

谁能给我解释一下?

【问题讨论】:

  • 来自codeigniter.com/user_guide/libraries/sessions.html#flashdata:CodeIgniter 支持“flashdata”,即只有下一个请求可用的会话数据,然后自动清除。
  • 是的,我知道,但我的问题是第一次当这个函数将在那个时候加载它不会进入,否则它将重定向我而不是加载视图。因此,在加载视图后,如果我按 F5 或浏览器刷新按钮,则再次调用函数并加载正确的视图,但这次它显示的是在 if 条件中给出的 flashdata,但 if 条件没有运行,所以为什么会这样显示此消息?如果我按 CTRL+F5 则不显示 flashdata
  • 那么您是否编写了任何测试代码来仔细检查这一点?

标签: php codeigniter codeigniter-3


【解决方案1】:

这不是解决方案,而是更多的调查来满足自己的工作方式......

因此,当事情变得“混乱”时,总是回归基础的好时机。因此,当您确信您的代码执行正确但给您意想不到的结果时,这是一些测试代码,只是为了检查 flashdata 的行为。

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!";
        }
    }
}

注意:这将独立运行,而不依赖于您现有的代码。

【讨论】:

    【解决方案2】:

    the CI manual says:

    CodeIgniter 支持“flashdata”或会话数据 可用于下一个请求,然后自动清除。

    F5 正在发送一个新请求,所以 flashdata 被清除

    【讨论】:

    • 是的,我知道,但我的问题是第一次加载此函数时,如果条件不进入,它将重定向我而不是加载视图。因此,在加载视图后,如果我按 F5 或浏览器刷新按钮,则再次调用函数并加载正确的视图,但这次它显示的是在 if 条件中给出的 flashdata,但 if 条件没有运行,所以为什么会这样显示此消息?如果我按 CTRL+F5 则不显示 flashdata
    • 不进入if条件如何设置flashdata?
    【解决方案3】:

    不要使用 $this->session->set_flashdata(),改用 $this->session->mark_as_flash(),它对我有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 2014-04-27
      • 2013-04-28
      • 1970-01-01
      • 2013-07-28
      相关资源
      最近更新 更多