CodeIgniter Forums
master controller for common tasks? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: master controller for common tasks? (/thread-40536.html)



master controller for common tasks? - El Forum - 04-11-2011

[eluser]Random dude[/eluser]
I remember reading on the CI forums once about someone using a "master" controller that was invoked before all other controllers and performed common tasks - especially user login check.

So this master controller acts as a sort of gateway between an unknown user and the rest of your controllers - the alternative is to have login check code in each of your controllers?

What are some good suggestions for this problem using CodeIgniter?

Note: I'm using Ben Edmonds IonAuth by the way so maybe others that have used that could share their solution.


master controller for common tasks? - El Forum - 04-12-2011

[eluser]cryogenix[/eluser]
Add this at the bottom of application/config/config.php:

Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon.
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
  if(strpos($class, 'CI_') !== 0)
  {
    @include_once( APPPATH . 'libraries/'. $class . EXT );
  }
}

then make custom controllers in your application/libraries folder like this Protected_Page_Controller:

Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  class Protected_Page_Controller extends CI_Controller
  {
    public function __construct()
    {
      parent::__construct();

      if(!$this->ion_auth->logged_in())
        redirect('auth/login', 'refresh');
    }
  }
/* End of file Protected_Page_Controller.php */
/* Location: ./application/libraries/Protected_Page_Controller.php */

then finally, simply extend this with any other controller that needs the authentication check:
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  class Welcome extends Protected_Page_Controller
  {
    public function __construct()
    {
      parent::__construct();
    }
    
    public function index()
    {
      //do something here
    }
  }
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */



master controller for common tasks? - El Forum - 04-12-2011

[eluser]Random dude[/eluser]
Cheers cryogenix! A Godly solution! Actually using inheritance, wow! CI forums are so good!


master controller for common tasks? - El Forum - 04-12-2011

[eluser]cryogenix[/eluser]
no probs. don't mention it..

lol...

and i'm pretty sure that's just a normal solution and not Godly or whatsoever hehe Tongue i forgot where I got that native autoload snippet so credit goes to whoever he is...


master controller for common tasks? - El Forum - 04-12-2011

[eluser]InsiteFX[/eluser]
@cryogenix

You should leave Phil's name in the __autoload he is the one that wrote the autoloader code. Give reconition where it is do...

Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon.
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
}

InsiteFX


master controller for common tasks? - El Forum - 04-12-2011

[eluser]CroNiX[/eluser]
Another way is just to create a library that gets autoloaded. Those get executed before the controller.


master controller for common tasks? - El Forum - 04-12-2011

[eluser]cryogenix[/eluser]
@insitefx: oh so it's phil who did that... yeah thanks for the heads up


master controller for common tasks? - El Forum - 04-14-2011

[eluser]Random dude[/eluser]
@CroNiX I looked here: http://ellislab.com/forums/viewthread/98811/#499068,

When using an autoloaded library - if login authorization fails, will simply using the code

Code:
redirect('auth/login', 'refresh');

successfully stop the invokation of the target controller? and therefore can you safely at that point consider the unauthorized user has been blocked?

Because this seems simpler and cleaner than having to extend all controller by another class, not to mention using the autoload code. What do you think?


Cheers to cryogenix for the redirect code.