CodeIgniter Forums

Full Version: Loading Libraries at 'pre_controller' hook stage? (developing light-weight custom authentication)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]beyondwords[/eluser]
Background:
I am looking to develop a custom authentication module. I've looked into auth and FreakAuth but they both have a lot of code for public sites. I am developing an internal site with fairly unique sign-up, and access control needs. If I've over-looked existing authentication solutions which might be lighter, or more easily customized, I'm game for that too.

Problem:
I would like to do the authentication as early in the load process as possible. To avoid loading a protected controller, then the login controller.

I have begun work on my library, but hit the major road-block that
Code:
$CI = get_instance();
gives me this error when I try to run the hook (via Filter) as 'pre_controller':

Quote:A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: filters/auth.php
Line Number: 12

I change the hook that invokes filter before to 'post_controller_constructor' and my filter and library work just fine. BUT, after my authentication library loads, if the user is attempting access without a login, or to a restricted area, every library, from config on, gets loaded again to load a new controller.

I need to be able to check session data (using DB Session) in order to verify a user's access privileges.

Am I missing something here? Is there a way to load libraries 'pre_controller'?
I tried to use the load_class method which the Controller library ends up invoking eventually, but then this causes errors further down the line when a controller wants to load those libraries, and can't since they're already loaded, but don't have access to them.

Thanks for any insight!

El Forum

[eluser]Glen Swinfield[/eluser]
It is Controller() that calls CI_Base that allows get_instance() - before a controller is instantiated there is no get_instance().

You could try making your hook class extend Controller() and call parent::controller() in the constructor.

I'm 80% this is your only solution. :question:

El Forum

[eluser]stevepaperjam[/eluser]
Could all your controllers just extend a base controller?

The base controller would then do the authentication stuff for you.

(whoops, codepat beat me to it)

El Forum

[eluser]beyondwords[/eluser]
Yeah that all makes sense from the digging around the source I did to try and figure this one out. I was hoping to do something other than extending from Controller for something that isn't strictly speaking a controller. But I might try that if I can't think of a completely different take on this problem.

Thanks to you both!