Welcome Guest, Not a member yet? Register   Sign In
Using other controller to display a view
#6

[eluser]jedd[/eluser]
Ah, gotcha now.

Yes, you're right - you want to avoid putting that logic into the view per se - as there will be times when a controller/method (which pretty much should be the definition of your page, if not your layout) is just too busy, or it would be inappropriate, to display an extra login section.

So for this I think there are two MVC-friendly, code-lite, easy-to-read broad ways to handle it.

If your method of identifying if a user is logged in or not (ie. if they should see a login box) is really quite lightweight, then just deal with that line of code and the call to generate the view-ette inline. An example from my code:

Code:
// in my controller somewhere
if  ( $this-session->userdata('login_name') )
     // I'm logged in
else
     // I'm not.


Second, write a helper - I think most people end up with a helper file that is loaded all the time (with a handful of others that are used on demand). This is much the same as above, but far more readable. I don't think it's terribly expensive as you're referencing the extant CI object, not copying it. An example for your situation
Code:
// In controller
if (! is_logged_in() )
     // call in login view-ette
Code:
// In helper
function is_logged_in()  {
    $CI =& get_instance();
    return ($CI->session->userdata('login_name')) ? TRUE : FALSE ;
    } // end-function  is_logged_in ()
Code:
// Still in helper
// You could obviously have a small handful of these types of things
function is_admin()  {
    $CI =& get_instance();
    return ($CI->session->userdata('admin')) ? TRUE : FALSE ;
    } // end-function  is_admin ()


A variant on the second option is to utilise the MY_Controller approach, to get $this-> functions available to all inherited controller classes. In this instance I want to send them on a one-way street to the login page if they hit certain controllers without being logged in. This function could have been called 'kick_nonauthenticated_users'.
Code:
// In my forum controller's constructor (people must be logged in to get here)
     $this->_ensure_authenticated_user( "Forums" );

Code:
// In my MY_Controller library extension
    function  _ensure_authenticated_user ( $page_message = "this")  {
        if (! $this->session->userdata('login_name'))  {
            $this->session->set_flashdata('user_needs_to_login', $page_message);
            redirect('/people/login');
            }
        }


Am I getting closer?


Messages In This Thread
Using other controller to display a view - by El Forum - 04-02-2009, 06:31 PM
Using other controller to display a view - by El Forum - 04-02-2009, 06:53 PM
Using other controller to display a view - by El Forum - 04-02-2009, 07:17 PM
Using other controller to display a view - by El Forum - 04-02-2009, 07:37 PM
Using other controller to display a view - by El Forum - 04-02-2009, 09:29 PM
Using other controller to display a view - by El Forum - 04-03-2009, 04:35 AM
Using other controller to display a view - by El Forum - 04-03-2009, 12:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB