Redirect without calling MY_Controller again |
[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
[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/C...ing-it-DRY So you'd have something like: Code: class MY_Controller extends CI_Controller { Code: class Auth_Controller extends MY_Controller {
[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. |
Welcome Guest, Not a member yet? Register Sign In |