0
votes

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)

3

3 Answers

0
votes

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/

0
votes

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

0
votes

The solutions given where not to my likeing so i made one myself, with help of This topic

I created a library like

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Auth
{

  public function get_menu($data)
  {
    $CI =& get_instance();
    if ($this->is_logged_in())
    {
        return ($CI->load->view('templates/headerlogged', $data));
    }
    else
    {
        return ($CI->load->view('templates/header', $data));
    }
  }

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

In my /application/config/autoload.php i add

$autoload['libraries'] = array('auth',);

to make sure i can always use the code in my controllers without having to load them manually

In my controllers i can now use

$this->auth->get_menu($data);

instead of

this->load->view('templates/header', $data);

and i always get the correct menu.

I know the name auth is odd but you can change it to your likeing.