0

I've done quite a bit of research and haven't found a satisfying answer.

How should I use CodeIgniter libraries such as tank auth? I've found a handful of ways, but they all seem sort of lackluster:

  1. Do I use the controller mostly as is, adding controller functions/including styles as needed?
  2. Do I use the controller as an example to model my own after, relying on calls to $this->tank_auth and the views included with tank auth?
  3. Or do I extend MY_Controller with the tank-auth controller, then extend that for any particular controller that needs authentication, and just call parent::login() (or register(), activate(), etc )?

The first option seems the best, but it seems like it would be difficult to avoid copying a lot of code (what happens if I want a login form, but don't want to redirect to /auth/login?)

The second option has the same problem, but worse. I would need to include the logic of the tank auth controller's login function each time I wanted to use the login_form view, correct?

The last seems really hacky and seems anti-MVC to me, but maybe I'm wrong.

Stephen Panzer
  • 289
  • 5
  • 16

1 Answers1

1

Check out http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

Pseudocode (see the link - really good example there):

application/config/config.php

$autoload = array('tank_auth');

// add this to the bottom of config.php as per the directions in the link
function __autoload($class)
{
     if(strpos($class, 'CI_') !== 0)
     {
        @include_once( APPPATH . 'core/'. $class . EXT );
     }
}

application/core/Private_Controller.php

class Private_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        if(! $this->tank_auth->is_logged_in()){
            redirect('auth/login');
        }
    }
}

Controller

 class PrivateStuff extends Private_Controller {
     function index() {
         echo "you are logged in";
     }
 }

-

http://example.com/index.php/privatestuff/
>you are logged in

As far as views, you can use the ones that came with the lib as they are, customize them, or create your own -- up to you.

stormdrain
  • 7,915
  • 4
  • 37
  • 76