这个“$this -> session -> userdata("datiPreventivo");”究竟是做什么的?进入 CodeIgniter 控制器 class?

What exactly does this "$this -> session -> userdata("datiPreventivo");" into a CodeIgniter controller class?

我是一名 Java 开发人员(我经常使用 Spring MVC 在 Java 中开发 MVC web 应用程序),对 PHP 了解甚少,我有在使用 CodeIgniter 2.1.3.

的 PHP 项目上工作

所以我对这个控制器方法究竟如何工作有以下疑问:

所以我有这个 class:

class garanzieValoreFlex extends CI_Controller {

    ..................................................... 
    ..................................................... 
    ..................................................... 

   public function index() {

        $this->load->model('Direct');
        $flagDeroga = "true" ;

        $this->session->userdata("flagDeroga");

        $data = $this->session->userdata("datiPreventivo");
        $this->load->model('GaranzieValoreFlexModel');

        $data = $this->session->userdata("datiPreventivo");
        $this->load->model('GaranzieValoreFlexModel');

        $this->load->view('garanziavalore/index_bootstrap',$data);
    }

}

我知道 index() 控制器 garanzieValoreFlex 方法 class 处理向 URL: http://MYURL/garanzieValoreFlex 并显示 /views/garanzievalore/index_bootstrap.php 页面。

它工作正常。我唯一不明白的是这个代码行到底是做什么的:

$data = $this -> session -> userdata("datiPreventivo");

你能帮我看看到底在做什么吗?我认为它是将某些东西放入 HttpSession 或类似的东西,但我对此绝对不确定,我无法理解其中的逻辑。

session 是一个 Codeigniter (CI) 库 (class),它允许数据在来自浏览器的多个页面调用中保持不变。在您使用的 CI 版本中 "native" PHP 未使用会话功能。但是 CI 的 session class 确实模仿 PHP 的会话,因为数据存储在 PHP 关联 array.

class 有许多不同的方法来存储和检索用户定义的数据。函数 userdata("index_to_data") 是主要的 class 方法之一。它用于检索已存储在 session class 中的数据。

传递给 userdata() 的参数是 session class 数组 $userdata 中值的键。因此,$this->session->userdata("datiPreventivo"); returns 存储在 $userdata["datiPreventivo"] 的值。如果密钥(在本例中为 "datiPreventivo")不存在,则 $this->session->userdata("datiPreventivo") returns FALSE

在您使用的代码中的某处,您会发现一行数据存储在会话中。这行代码可能看起来像这样。

$newdata = array("datiPreventivo" => $something_value);
$this->session->set_userdata($newdata);

在您的代码中搜索“$this->session->set_userdata”可能有助于了解为将来的页面加载保存的内容。

重要的是要知道 CI 的 session class 在版本 > 3.0 中被完全重写,所以 current documentation may not be very helpful to you. You will need to find the documentation for the version you are using to learn more about the session library. I believe that documentation is included in the download for your version which can be found here.