CodeIgniter Forums
is this ok in a view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: is this ok in a view? (/thread-11019.html)



is this ok in a view? - El Forum - 08-22-2008

[eluser]Fenix[/eluser]
With the MVC pattern, is it ok to put something like this in a view?

Code:
if logged in
    show logout link
else
    show login link
end if

More specifically, is it ok to actually check the session for things like this right in the view? Something like:

Code:
if ($this->session->userdata('id') >0)
{
    // show logout
}
else
{
    // show login
}



is this ok in a view? - El Forum - 08-22-2008

[eluser]LuckyFella73[/eluser]
I think, if the status logged in or not would only change
a small part of the view it's no problem. Maybe it would be more
strict to check the status in the controller, store the
link (the HTML part) in a variable and just echo the variable
in the view. Would be a bit more flexible.


is this ok in a view? - El Forum - 08-22-2008

[eluser]Adam Griffiths[/eluser]
I do this inside the controller. It would be best to put it in a controller, and put the user details/logout link etc in one view file, and the login form in another view file. That way it would be easier to find things and change them at a later date.


is this ok in a view? - El Forum - 08-22-2008

[eluser]fesweb[/eluser]
Even better? Create a function in a/the model which checks the login status and writes the link.

This is probably horribly wrong to do...
Code:
function check_login() {
  if logged in
    $log_link = '<a href="logout">Logout</a>';
  else
    $log_link = '<a href="login">Login</a>';
  end if
  return $log_link;
}
...but you could do something like this instead...
Code:
function check_login() {
  if logged in
    $log_view = 'utilities/logout_link';
  else
    $log_view = 'utilities/login_link';
  end if
  return $log_view;
}
...then, in the controller(s)...
Code:
$this->load->view($this->your_model->check_login());
Either way, if the link or wording changes later, you just change it one in the one function or view.


is this ok in a view? - El Forum - 08-22-2008

[eluser]Fenix[/eluser]
Ok, I was just thinking that the header view (where the link would go) is going to be on every page so it would get annoying having to check the login status every time you load a view...

something like...
Code:
$data['logstatus'] = $this->session->userdata('id');
$this->load->view('template_view',$data);
every time...