Welcome Guest, Not a member yet? Register   Sign In
Display different views depending on whether user is logged in - Best way?
#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...


Messages In This Thread
Display different views depending on whether user is logged in - Best way? - by El Forum - 05-21-2008, 01:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB