One if statement to rule them all - 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: One if statement to rule them all (/showthread.php?tid=22414) |
One if statement to rule them all - El Forum - 09-08-2009 [eluser]dennismonsewicz[/eluser] Is there anyway to write the following without having to write it in each function? Code: function main() { I am interested in not have to write the if 'logged_in' == TRUE everytime I write a new function in the admin section... how would I write this once in my parent function? One if statement to rule them all - El Forum - 09-08-2009 [eluser]brianw1975[/eluser] search for MY_Controller or extending Controller One if statement to rule them all - El Forum - 09-08-2009 [eluser]dennismonsewicz[/eluser] ok... but what would my if else look like? if logged_in == true do what? the else would just redirect them home but what would the if statement do if evaluated true? One if statement to rule them all - El Forum - 09-08-2009 [eluser]brianw1975[/eluser] simply reverse your logic... Code: if($this->session->userdata('logged_in') != TRUE) One if statement to rule them all - El Forum - 09-08-2009 [eluser]bretticus[/eluser] You could still extend Controller, but it's also as easy as: Code: class something extends Controller { One if statement to rule them all - El Forum - 09-09-2009 [eluser]ggoforth[/eluser] You could also do it with hooks. I am currently using a hook to check the status of a session var, and redirecting to login if not logged in. Works well. I do like (and probably would have used) MY_controller if I had it to do over again, as it's a bit more flexible, but it's working now, so it's all good. Greg One if statement to rule them all - El Forum - 09-09-2009 [eluser]Phil Sturgeon[/eluser] +1 for MY_Controller. On PyroCMS I use several types of controller classes. MY_Controller adds logic to ALL controllers then I have Public_Controller and Admin_Controller to add shared logic to the front and back ends respectively. If you just want to do this on a per-controller basis or don't want the extra complication, just do this: Code: class something extends Controller { |