Redirect without calling MY_Controller again - 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: Redirect without calling MY_Controller again (/showthread.php?tid=60698) |
Redirect without calling MY_Controller again - El Forum - 06-06-2014 [eluser]Unknown[/eluser] I am looking for a way to basically redirect the URI on an error or if the user isn't logged in. The problem is when I use the redirect function I get caught in a loop as MY_Controller has the condition check again. So if I checked to see if the user was logged in and they weren't I would call redirect('login') to go to the login controller obviously which will call the MY_Controller again. Is there a way to just direct codeignitor to the Login controller without calling MY_Controller again? Maybe change the controller eternally or something? Thanks Redirect without calling MY_Controller again - El Forum - 06-06-2014 [eluser]CroNiX[/eluser] No, you can't bypass MY_Controller as any controller that extends MY_Controller will get called automatically. I'd create another base controller that extends MY_Controller, maybe Auth_Controller, and move your auth check to there. Then any controllers that require auth should extend that new Auth_Controller base controller. Here's an article to show how to do that and have multiple base controllers that can be used in different circumstances. http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY So you'd have something like: Code: class MY_Controller extends CI_Controller { Code: class Auth_Controller extends MY_Controller { Redirect without calling MY_Controller again - El Forum - 06-06-2014 [eluser]CroNiX[/eluser] Either that or put your auth check in MY_Controller. Any controller that requires auth should extend MY_Controller, and any controller that doesn't need auth still extends the original CI_Controller. |