master controller for common tasks? |
[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.
[eluser]cryogenix[/eluser]
Add this at the bottom of application/config/config.php: Code: /* 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'); 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');
[eluser]Random dude[/eluser]
Cheers cryogenix! A Godly solution! Actually using inheritance, wow! CI forums are so good!
[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 i forgot where I got that native autoload snippet so credit goes to whoever he is...
[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: /* InsiteFX
[eluser]CroNiX[/eluser]
Another way is just to create a library that gets autoloaded. Those get executed before the controller.
[eluser]cryogenix[/eluser]
@insitefx: oh so it's phil who did that... yeah thanks for the heads up
[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. |
Welcome Guest, Not a member yet? Register Sign In |