Reputation: 1114
I am using hooks in my CI application "pre_controller hook specifically".
But the problem is Hooks are activated each time a request is issued to any other controller even controllers that i don't want the hook to be activated in.
Can hooks be enabled for only one controller? just like the @Before annotations in playframework.
Thanks in advance.
Upvotes: 1
Views: 2417
Reputation: 92581
Why don't you put that logic in the Constructor of your controller?
If you have multiple controllers you wish to share this functionality, simply extend the CI_Controller
with a new class in application/core/MY_Controller.php
and put the functionality in there, then in the controllers have them extend that class instead of the default CI_Controller (you can add more than one class in MY_Controller.php
)!
Anything that you put in the override class in MY_Controller.php
would execute before the code in the rest of the controller, simulating the pre_controller
hook.
Just remember to call the parent constructor as well:
function __construct(){
parent::__construct();
}
See the manual for more info about extending the core: https://www.codeigniter.com/user_guide/general/core_classes.html
Upvotes: 8
Reputation: 174
You could also put your code into a library to use whenever you need it. I ended up using my solution because I could keep my authentication logic separate to my modules. It makes it easier for updating as well.
Upvotes: 0