Welcome Guest, Not a member yet? Register   Sign In
Display different views depending on whether user is logged in - Best way?
#1

[eluser]Dave S[/eluser]
Forgive me if this has been asked before. I have searched unsuccessfully for an answer before posting this.

I am trying to figure out the best way to display different views based on whether the user is logged in or not.

For example, in my header or sidebar, I want to list some features. If the person is logged in, they get access to those features. If not, they are redirected to a login / register page when they click. Also, in the header, display a small login form or a 'Welcome, username' message depending on their status.

I am very new to CI and having a hard time wrapping my head around some of the concepts. I have been learning php but most of my code was very linear.

These are some of the ideas I had about how to do this:
1) In every controller, check if they are logged in and pass html in a string to the view for the header.
2) In every controller, check login and pass a variable to the view ($logged_in) then do the logic in the view to decide which code to display. (if ($logged_in) display one thing else... etc)
3) Have 2 versions of header or sidebar views. One for logged in users and one for guests, and have the controller call the appropriate view depending on status.

Are any of these reasonable solutions or am I totally missing something? Any thoughts / ideas are greatly appreciated. Sorry for the long-winded post!
#2

[eluser]sikkle[/eluser]
You have many choice there, to keep it light, just build a helper that you can use to load view.

Or directly if your view, if you want to keep it realy light, can if-else your stuff there. then load the good view.

You also can build your layout "modular" to make use of partial and stuff, you should look into the forum about HMVC.

Hope this short answer help.
#3

[eluser]Mirage[/eluser]
Like sikkie said - lots of ways to do it. But I agree that a view helper is the most flexible option, because it makes it independent from your controller. Unless the header or sidebar layout is vastly different across uses, I'd just put the logic in the view template and switch things on|off depending on the credentials. So:

- create a helper
This helper will collect the credentials (probably from the session), then load a view[fragment] representing the header or sidebar template and return it as a string

- create view fragment template[s]
For each fragment subject to this, create a template that contains the view logic to adjust itself

- in you main view (the one loaded by the controller) place echo statements calling the helper functions.

You should probably autoload the helper. Examples:

File view_helper.php
Code:
function pageheader() {
    $CI=&get;_instance();
    $loggedIn = $CI->session->userdata('loggedin');
    $privs = $CI->session->userdata('userprivs');
    $profile= $CI->UserModel->getProfile();
    return $CI->load->view('path/to/header',compact('loggedIn','privs','profile'),true);
}

function sidebar() {
    $CI=&get;_instance();
    $loggedIn = $CI->session->userdata('loggedin');
    $privs = $CI->session->userdata('userprivs');
    $profile= $CI->UserModel->getProfile();
    return $CI->load->view('path/to/sidebar',compact('loggedIn','privs','profile'),true);
}

Template header.php
Code:
<? if($loggedIn):?>
Welcome, <?= $profile->name ?>
<? else:?>
<a href="/login">Click here to log in</login>
&lt;?endif;?&gt;

Repeat something similar for the sidebar view. Finally your 'page' view:

Template: somepage.php
Code:
...
<div id="header">&lt;?= pageheader() ?&gt;</div>
<div id="navigation">&lt;?= sidebar() ?&gt;</div>
...

Hope this helps...
#4

[eluser]Dave S[/eluser]
Thanks for the replies! That'll get me going in the right direction.
#5

[eluser]sikkle[/eluser]
Also, helper is light atm, if you build basic normal application, don't try to build an empire just to show a partial.

good luck with your project!
#6

[eluser]Bruno De Barros[/eluser]
@CI Mirage, that is one question I was wondering about, before migrating to CodeIgniter. Now that you clarified it, that's one less thing in my mind sotpping me from using it. For my next project, I will start using CodeIgniter for real Smile. Thanks Wink.




Theme © iAndrew 2016 - Forum software by © MyBB