![]() |
"Security interceptor" and interceptors in general - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: "Security interceptor" and interceptors in general (/showthread.php?tid=32974) |
"Security interceptor" and interceptors in general - El Forum - 08-11-2010 [eluser]eddieconnecti[/eluser] Tried the search, but couldn´t find anything helpful about his: In some other frameworks i worked before, i used interceptors to check whether the user is logged in or not. I used them to handle pre-requests, like calling models for the navigation tree before the view is called and any other controller methods are called. So what i am looking for is a way to call methods each time a new request starts. In this way i want to call a model, recieve an array of navigation items, push this to the $data object of the controller so the view knows which navigation items to show. On the same way i want to check on every request if the user is logged in and if he has permissions for the current action. If not, redirect the user to another page. The advantage is to not write the hole code in every controllers method but having the code in external classes which are automatically called. Maybe it is possible to do this with hooks or callbacks, but i like the way to have a class for security functions (like a security interceptor) and a class for navigational purposes. Thanks! "Security interceptor" and interceptors in general - El Forum - 08-11-2010 [eluser]mddd[/eluser] Build a library and put your call to that library in the constructor of your controllers. It will be called in every request. If you don't want to write a line in each controller, you can extend the default controller (make it a MY_controller) and put the call in there. Then you let every controller extend from MY_Controller instead of Controller. "Security interceptor" and interceptors in general - El Forum - 08-11-2010 [eluser]eddieconnecti[/eluser] Thanks! Sounds like extending the controller class could do the job. Quote:Build a library and put your call to that library in the constructor of your controllers I don´t know what you mean? How can that be done? Maybe you could post a snippet of code? "Security interceptor" and interceptors in general - El Forum - 08-11-2010 [eluser]eddieconnecti[/eluser] The following code shows how i now tried to extend the base ci controller class: Code: class MY_Controller extends Controller { The file is named MY_Controller.php and is located in system/libraries/ In the controllers i use following syntax: Code: <?php Code: <?php $this->load->view( 'default/header' ); ?> Maybe i have to bubble the $data variables into subviews? Something like Code: ... "Security interceptor" and interceptors in general - El Forum - 08-11-2010 [eluser]mddd[/eluser] Once variables are loaded in one view, they are available in all subsequent views. So that is not the problem. I understand that $data['menu'] is something you want loaded in every controller. That's why you put it in the MY_Controller class. But you probably have a index() method in your controllers. Like Users has an index method, right? That means it overwrites the index() you made in MY_Controller. And so, the $data['menu'] is still not set! If you want $data to "automatically" contain information, you would have to do something like Code: class MY_Controller extends Controller { And in the Controller: Code: class Users extends MY_Controller { "Security interceptor" and interceptors in general - El Forum - 08-11-2010 [eluser]eddieconnecti[/eluser] That´s perfect! Thanks a lot! |