Check a function before doing anything? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Check a function before doing anything? (/showthread.php?tid=61392) |
Check a function before doing anything? - Hamed - 04-12-2015 Hello, I want to check a function (for example $this->auth->is_login()) before doing anything in controller? Note: 1- I check it in any control 2- If false goto login else view page. How can I have this without code in my controllers? RE: Check a function before doing anything? - nasser.man - 04-12-2015 i think you can do this : - create a controller in /core/ that extends CI_Controller and call your function in constructor function of that class, - your other controllers must extend your controller class, example , i use this base class for my project, notice you must use MY_ (or any that you set in config) instead of N2_ Code: class N2_Controller extends CI_Controller { and your other controllers : Code: defined('BASEPATH') OR exit('No direct script access allowed'); RE: Check a function before doing anything? - casa - 04-12-2015 Do you want to check if a function exists or if the result of your function exists ? I say that because your example looks like checking if $this->auth->is_login() == TRUE. If you want to check if a method in a class exists, you can use method_exists() or is_callable(). Be careful : method_exists() will return true (or false) for a private/public/protected method whereas is_callable() returns true (or false) for only public method in a class and all method like __call. RE: Check a function before doing anything? - chrismartinez99 - 04-22-2015 I used a 'post_controller_constructor' hook to do this: Hook in config/hooks.php: PHP Code: $hook['post_controller_constructor'] = array( Check Function in hooks/Auth.php: PHP Code: public function check() See the docs for reference. Good luck. |