Welcome Guest, Not a member yet? Register   Sign In
Redirect without calling MY_Controller again
#1

[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
#2

[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 {
  public function __construct()
  {
    parent::__construct();
  
    //code here that will be called for all controllers extending MY_Controller
  }
}

Code:
class Auth_Controller extends MY_Controller {
  public function __construct()
  {
    parent::__construct();

    //code here for auth check.  All controllers that require auth extend Auth_Controller
  }
}
You'd need to add an autoload to do this. See Phils article.
#3

[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.




Theme © iAndrew 2016 - Forum software by © MyBB