I have successfully extended the core using the MY_Controller as described in CI's documentation.
This way I can put some repetitive functions (ie, auth check) in the constructor of MY_Controller so they always run before methods from my other controllers.
My issue now is that some parts of my webapp are open (ie, don't require login) and others require login.
Therefore, I cannot extend ALL my controllers from MY_Controller (which contains an auth check function).
I wondered if it would be possible to extend the core so as to have, say, LOG_Controller and NOLOG_Controller.
Then, controllers that require login would extend from LOG_Controller --- and controllers that do not require login would extend from NOLOG_Controller.
Is this posssible? (or is it bad form?)
It seems config/config.php only allows for one core extension prefix so I'm not sure it's possible.
Let me know what you think or if there's a better way of doing this. Thanks.
Have a look at this article http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY - It is exactly what you are looking for.
I use the following trick. Define all your base controllers in My_Controller.php:
<?php
class My_Controller extends CI_Controller{
}
class LOG_Controller extends My_Controller{
}
class NOLOG_Controller extends My_Controller{
}
?>
This makes all defined controllers available later in your controllers. Just avoid making the base controllers too fat.
Also you can check this example: https://github.com/aquilax/novigeroi2/blob/master/application/core/AQX_Controller.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With