简体   繁体   中英

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

I am a Java developer (I often used Spring MVC to develop MVC web app in Java) with a very litle knowledge of PHP and I have to work on a PHP project that use CodeIgniter 2.1.3 .

So I have the following doubt about how exactly work this controller method:

So I have this 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);
    }

}

I know that the index() method of the garanzieValoreFlex controller class handle HTTP Request toward the URL: http://MYURL/garanzieValoreFlex and show the /views/garanzievalore/index_bootstrap.php page.

It works fine. The only think that I can't understand is what exactly does this code line:

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

Can you help me what exactly is doing? I think that it is putting something into the HttpSession or something like this but I am absolutly not sure about it and I can't understand the logic.

session is a Codeigniter (CI) library (class) that allows data to persist across multiple page calls from a browser. In the version of CI you are using "native" PHP session functionality is not used. But CI's session class does mimic PHP's session in that data is stored in a PHP associative array .

The class has many different methods to store and retrieve user defined data. The function userdata("index_to_data") is one of the main class methods. It is used to retrieve data that has been stored in the session class.

The argument passed to userdata() is the key to a value in the session class array $userdata . So, $this->session->userdata("datiPreventivo"); returns the value stored at $userdata["datiPreventivo"] . If the key (in this case "datiPreventivo") does not exist then $this->session->userdata("datiPreventivo") returns FALSE .

Somewhere in the code you are working with you will find a line where data is stored in the session. The line of code might look something like this.

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

Searching your code for "$this->session->set_userdata" might be helpful to understand what exactly is being saved for future page loads.

It is important to know that CI's session class was completely rewritten in versions > 3.0 so the 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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