0

I am making first website using codeigniter framework. I have made a user class with a method to check if a user is logged in like :

public function is_logged_in()
{
    $this->load->library('session');
    $loggedin = $this->session->userdata('logged_in');
    if ($loggedin)
    {
        return TRUE;
    }
    else
    {
        return FALSE;   
    }
}

Now i have a menu in my header with a button which needs to change depending on the login status. If the user is not logged in i want to show a login button, else a logout button (with the right links of course). I think in MVC there should be only html and only php where needed. Where should i place the check to see if i show the login or logout button, should i do it in every controller where i call the header like:

 $data['menubutton']= $this->getbutton();//something to get the button
 $this->load->view('templates/header', $data); 

If i would do it that way i need to add it to every controller, should i just do the check in the view, or is there another simple solution.

Question in short what is the best way/position to create the correct menu button depending on login status? (login or logout button)

tereško
  • 58,060
  • 25
  • 98
  • 150
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

3 Answers3

0

In short Answer: You can do it in your view page.

In more advanced way: You can use two different layout for guest user and logged-in user. There may be many more difference between them, like viewing user information(user name, greetings, profile link etc).

NB: Finding out If the user is logged-in or not is not part of view. But view can act on single condition check

xiidea
  • 3,344
  • 20
  • 24
0

You can extend your controller by placing a file in application/core called MY_Controller.php

<?php

class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        // Perform the login check here
    }
}

?>

In your usual controllers you would extend the MY_Controller instead of CI_Controller

class User extends MY_Controller { }

Because you are now extending My_Controller instead of CI_Controller you can place code in the MY_Controller __construct() method to execute on every controller load, this is a perfect place to put things like login checks which you execute in every controller, but do not want to duplicate the code.

If I were implementing this I would simply have a user object or boolean which I can check in my views, and simply do an if statement to display the correct button. I would keep the button code in the views.

More information on extending core classes here on CodeIgniters documentation: http://ellislab.com/codeigniter/user-guide/general/core_classes.html

A more friendly description of the uses of extending the core controller can be found here: http://www.gregaker.net/2011/mar/18/extending-codeigniters-controller/

AJReading
  • 1,193
  • 20
  • 35
  • Just simply store the outcome of your login check in a variable to pass to your view such as: `$this->data['logged_in'] = my_login_check();` and then pass that to the view when you load it `$this->load->view('my_view', $this->data);` so you then have access to the `$logged_in` variable in your view. – AJReading Mar 30 '14 at 03:25