Welcome Guest, Not a member yet? Register   Sign In
Loading Controllers


I feel like what I'm trying to do is load a controller from another controller. Maybe I should be trying to do something else to achieve the same results. If so, please let me know. Otherwise, maybe you could let me know how to do what I am trying to do.

For example, I have a controller that would build a home page (let's call it Home controller). I have another controller that builds a login page (let's call it Login controller). When a user submits the login form, they are redirected to the Login controller which handles determining successful login. On failure the Login controller would again build the login page but on success I want to build the homepage. Shouldn't the Home controller handle this? So it seems that the Login controller would then need to direct to the Home controller. How do I do that?

Am I going about things all wrong?


I normally write an authentication library for this, that checks whether a user is logged in. If he is not, the library redirects him to the login page. I call the check_logged_in() function in every controller that needs to be behind login.

The reason I place this logic in a library is that this way I can use it anywhere I like and if I whish to change the procedure I only have to do this once, in the library.

Example controller
function __construct() {
    // Retrieve logged in user data. If visitor is not logged in they are redirected to login page
    $this->data['user'] = $this->auth->check_logged_in();

You can extend this further by passing a role that a logged in user must have to access a certain controller or method.
$this->data['user'] = $this->auth->check_logged_in('administrator');

Ok, so the line of code that actually redirects the user to a specific controller looks like what?

redirect ("/user/login");

Note that I usually make the login stuff part of my user controller - that includes things like showing a user's public profile, allowing a user to modify their settings, as well as logout and login pages.

Looks good but I don't understand where the redirect method exists.

I get the following error:

Fatal error: Call to undefined method redirect()

Nevermind, I needed to load the URL helper.

Here's a handy hint.

Bookmark this page: [url="http://ellislab.com/codeigniter/user-guide/"]http://ellislab.com/codeigniter/user-guide/[/url]

Use its search function whenever you want to find what a given function does (or how you can load it) or to search for a function that does something in particular.

Theme © iAndrew 2016 - Forum software by © MyBB