Welcome Guest, Not a member yet? Register   Sign In
Sending session data to views
#1

[eluser]nuwanda[/eluser]
Hi

I've seen quite a few user login/session screencasts that restrict access to whole pages based on the user login status. That's easy as the controller can determine the status before it loads the view.

What I'm after is the best way to provide a variable to a view that the view can then use to show or restrict discrete page elements.

My solution at moment is to create a class property (member variable) and load that with the login status in the constructor. That array is then passed, along with other data, to the the view.

Code:
class Site extends Controller{

  public $data=array();
    
  function __construct()
    {
    parent::Controller();
    $this->data['logged_in']=$this->session->userdata('logged_in');
  }
  
  function index(){
    $this->data['content']='home';
    $this->load->view('template',$this->data);
  }

}


Seems to work fine but of course I have to do that for all controllers.

Is this the best solution or is there another way?

Thanks in advance.
#2

[eluser]adamp1[/eluser]
Could you not make a helper method to do this.
Code:
function logged_in()
{
    $CI =&get;_instance();
    return $CI->session->userdata('logged_in');
}

And then call that from the view instead? Then you don't need to extend all controllers, don't need to pass this variable to the view everytime.
#3

[eluser]nuwanda[/eluser]
Thanks, RA.

But I can't load a helper from the view, right? I'd still need to call the helper and add that to the $data sent to the view. Yes?
#4

[eluser]adamp1[/eluser]
No, Just add the helper to the autoloader so its always available. And then in your view just do:
Code:
if(logged_in())
{
    <code you want to gaurd>
}

And if you want to guard some HTML just do
Code:
&lt;?php if(logged_in()):?&gt;
<p> Special HTML only logged in users can see</p>
&lt;?php endif;?&gt;
#5

[eluser]nuwanda[/eluser]
Ok, thanks.
#6

[eluser]mddd[/eluser]
I would put all of the stuff that has to do with sessions (logging in, logging out, emailing a password reset link if someone forgot it, etc.) in a model.
The model would also contain a function to check if the user is logged in (by looking at the session data).
I think this is more elegant because it means that you can extend or change the way the user identification works, without having to alter your controllers.
All the controller needs to know is to check 'login_model->isloggedin()' if it wants to know if the user is logged in.

Put the model in the autoload config. Your controller could then be as simple as:

Code:
class Site extends Controller{

function __construct()
{
   parent::Controller();
   // do some more stuff here for this controller
}
  
function index()
{
   $data = array(
   'content'=>'home',
   'loggedin'=>$this->login_model->isloggedin()
   );
   $this->load->view('template', compact($data);
}

}
#7

[eluser]nuwanda[/eluser]
Thanks, LA.

I'd still have to have that code in each controller before I called the template. No?

I tried RA's solution of a helper. I've set it to autoload and now I'm calling the function in my views.

Am I missing something?
#8

[eluser]mddd[/eluser]
There are multiple ways to achieve the same thing. I always try to keep things as organized as possible.

The solution of creating a helper will work fine if you do it right. But I think it is not very organized. The helper will only work
correctly if the session library is also active. In my opinion, helpers should be functions that do not rely on other things being
loaded first. This helper is simply a 'shortcut' to some functionality that should be in a model.

Or let me put it his way: helpers are really part of the View layer of the MVC principle. By using a helper for this, you are
inserting Model functionality into the View part. That is bad, because models and views should function as separate from each
other as possible. That's why it is better to put this kind of intelligence into a model, and then use the controller to bring
everything together.
#9

[eluser]nuwanda[/eluser]
Thanks, LA, I understand what you're saying.




Theme © iAndrew 2016 - Forum software by © MyBB