Welcome Guest, Not a member yet? Register   Sign In
Redirect from a post_controller hook?
#1

[eluser]christatedavies[/eluser]
I have put in place a post_controller hook. This is for checking user login. I had to use post_controller as I needed access to the session data

Now, this all works fine, BUT, when it discovers a request for a non logged in user, I want to be able to redirect them to a log in page.

How do I do this since the controller is already loaded? I would guess the best place would be in the pre_controller but then I can't access session....

Thanks, Chris
#2

[eluser]boltsabre[/eluser]
I'd just make a helper function and do it in there, if not logged in then redirect the user direct from the helper. Lets call the function "logged_in()" for arguments sake.

Then in any controller/method that should be restricted you can put this as the first line of code (just don't forget to either autoload the helper, or load it manually before you call this function):

logged_in();

And bam, if not logged in they are redirected!
#3

[eluser]christatedavies[/eluser]
Thanks, that is a solution, and one I could do, but I really want it as a "hook" so I don't have to enter logged_in() everywhere in the code

This is going to be a large application and surely thats the whole point of a modular MVC application?

I know that Zend Framework has such a method. You write a controller plugin and its run before ANY controller... It \must\ be possible in Codeigniter...?
#4

[eluser]Aken[/eluser]
You can use a MY_Controller instead of a hook. Or, use the "post_controller_constructor" hook.
#5

[eluser]boltsabre[/eluser]
Ah right... sorry, I was under the impression that only certain pages were restricted (ie, forms, user profile, etc). But yes, if the the entire site is locked down then go with what Aken said, it's the way to go!

Or you may be interested in this article by the wizard Phil Sturgeon, about creating two different "sections" of your site (for example, front-end and back-end, or it can be easily adapted into something like public and private), but creating two different "base controllers" and having your various other controllers extending one of them. So, you could make a base "private" controller and lock down half of your site in it's constructor. Anyway, I'll let you read the article, he's a bit more articulate than me :-)
http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY




Theme © iAndrew 2016 - Forum software by © MyBB