views:

29

answers:

1

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

+1  A: 

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>
DRL
so, direct access is not possible?
helle
Yes it is as long as you have either loaded the session class in your controller or you have added it to your autoload.php list
DRL
would you suggest better to handle it in the controller, and write the needed parameters to the page-data variable, for clean coding?
helle
I usually access it directly from the view; i think its just a matter of preference
DRL