• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing session array from embedded view

#1
[eluser]FrankStar[/eluser]
I'm using template views with embedded content views inside. So far so good; everything works pefectly. Now when a user logs in, I store the useful data in my session so it can be shared through the entire site. Then instead of calling the session data directly from the template view, I pass it through the controller like this:

Code:
$data['main_content'] = 'pages/profile'; // The subview with the content
$data['userdata'] = $this->session->userdata; // The logged in user info
$this->load->view('mytemplate', $data);

However, as you can imagine this can become quite repetitive since I have to call this for every single pages.

How would you approach this situation? I don't have lot of experience using CI and designing login systems so any input would be greatly appreciated!

#2
[eluser]Samus[/eluser]
[quote author="FrankStar" date="1334599166"]I'm using template views with embedded content views inside. So far so good; everything works pefectly. Now when a user logs in, I store the useful data in my session so it can be shared through the entire site. Then instead of calling the session data directly from the template view, I pass it through the controller like this:

Code:
$data['main_content'] = 'pages/profile'; // The subview with the content
$data['userdata'] = $this->session->userdata; // The logged in user info
$this->load->view('mytemplate', $data);

However, as you can imagine this can become quite repetitive since I have to call this for every single pages.

How would you approach this situation? I don't have lot of experience using CI and designing login systems so any input would be greatly appreciated![/quote]
Put it in your controller constructor.

Code:
function __construct() {
parent::__construct();
$data['userdata'] = $this->session->userdata;
}

#3
[eluser]FrankStar[/eluser]
It works when it's in the controller but not in the constructor. I got the error "Undefined variable: userdata". Something I'm doing wrong?

#4
[eluser]CroNiX[/eluser]
userdata() is a method, not a property/variable.

And instead of
Code:
$this->session->userdata;
You probably want
Code:
$this->session->all_userdata();

#5
[eluser]FrankStar[/eluser]
Thanks for the clarification. It probably helped but I still can't access the dynamic data set in the constructor from the view.

Controller's Constructor:
Code:
function __construct() {
  parent::__construct();

  // Keep the session data available for all controllers
  $data['session'] = $this->session->all_userdata();
}

Controller:
Code:
function index()
{
  $user_info = array(
   'name' => 'User Name'
  );

  $this->session->set_userdata($user_info);

  $data['main_content'] = 'home';
  $this->load->view('templates/split', $data);
}


View (template):
Code:
<?php print_r($session); ?>

Really not sure why this isn't working after reading the "Adding Dynamic Data to the View" section of the user guide :-S

#6
[eluser]CroNiX[/eluser]
Because the class constructor gets called first (where you are reading the data) but the session data doesn't get set until it hits the index(). It probably is being passed, but it is outputting boolean FALSE (because data isn't set). Try using var_dump() instead of print_r() to see if that is the case. However, session data is global. You should be able to access it directly in your view without having to pass it.

#7
[eluser]Samus[/eluser]
Ahhh I didn't realize that.

The only reason I could think of is that you're storing the current session in the $data[] array (which is blank) before even giving your session any data, so it'll always be blank..

You could just call it straight in your view template, I personally don't think that's breaking any MVC 'rules'.

#8
[eluser]CroNiX[/eluser]
Also, the way it's written, $data isn't a class property, (it's just a method variable only existing in the method) so in index(), it doesn't know anything about $data you set in the constructor.

I'd remove the session stuff from the constructor.
I'd set the session data in index(). (just like you are)
I'd read the session data in the view.

In View:
Code:
$session_data = $this->session->all_userdata();
echo $session_data['name'];

It's kind of a waste of memory/processing passing the session data to the view as it already exists globally.

#9
[eluser]FrankStar[/eluser]
Ok thanks for the quick feedback guys, your explanations make a lot of sense. I guess I'll go back to calling the session variables directly in the view like so:

Code:
Welcome <?php echo $this->session->userdata('name'); ?>

Wish there was a cleaner way but at leats it works!

#10
[eluser]CroNiX[/eluser]
It's not really that different than calling $session['name'] in the view, which is how you would have had to do it if the first way worked.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.