Welcome Guest, Not a member yet? Register   Sign In
Starter help
#3

[eluser]CroNiX[/eluser]
/application/core/MY_Controller.php (Goes in /application/CORE (because you are extending /system/core/Controller.php), not a Library)

Code:
class MY_Controller extends CI_Controller {

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

    //load your auth library/whatever (if it isn't autoloaded) and do your login check
    $this->load->library('your_auth_library');
    $this->your_auth_library->check_login();
  }

  //create another method that will be available to any controller that extends this controller
  function verify_user($user_id = 0)
  {
      echo 'This is the verify_user method from MY_Controller';
  }
}

All other controllers that you want to have auth checked should now extend MY_Controller instead of CI_Controller....

Code:
class Your_controller extends MY_Controller {

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

    function index()
    {
      echo 'If you are seeing this, you are logged in';

      //test the other method
      $this->verify_user();
    }
}
going to http://yoursite.com/your_controller should now perform the login check.

Now, whenever you go to this controller, it will first load the MY_Controller and since the auth check is in it's construct, that will be executed every time before continuing on to your actual Your_controller.

If you have a controller that you DON'T want auth checked, have that controller extend the regular CI_Controller instead of MY_Controller.

You can take this a step further and have several "base controllers" (which is what MY_Controller is). You can have a different one for Front End, Back End, whatever, depending on the needs of your project. Here's a good article on how to take it one step further: http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY


Messages In This Thread
Starter help - by El Forum - 09-06-2013, 02:03 PM
Starter help - by El Forum - 09-07-2013, 04:30 AM
Starter help - by El Forum - 09-07-2013, 10:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB