Search code examples
codeigniternotation

is there a better, more lightweight way to access session data in codeigniter view?


Is there a more lightweight way to access session data in a view with codeigniter than posted here?

i think about soemthing like {session.myparameter}.

thanks for help


Solution

  • Assign your session data to an array in your controller that pass it to the view with the rest of the page data.

    $page_data['session_data']  = array(
        'session_param_1' => $this->session->userdata('session_param_1'),
        'session_param_2' => $this->session->userdata('session_param_4'),
        'session_param_3' => $this->session->userdata('session_param_3'),
        'session_param_4' => $this->session->userdata('session_param_2')
    );
    
    $this->load->view('your_view', $page_data);
    

    To access the session class directly from a view you must ensure that the session library has been loaded by the calling controller, or has been autoloaded in application/config/autoload.php

    $autoload['libraries'] = array('database', 'session');
    

    Then access in your view as required.

    <h2>Logged in as <?php echo $this->session->userdata('session_user_name'); ?> </h2>