CodeIgniter Forums
Loading Controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Loading Controllers (/thread-19118.html)



Loading Controllers - El Forum - 05-28-2009

[eluser]tommymcglynn[/eluser]
Hey,

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?

Thanks,
Tommy


Loading Controllers - El Forum - 05-28-2009

[eluser]JoostV[/eluser]
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
Code:
$this->load->library('auth');
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.
Code:
$this->data['user'] = $this->auth->check_logged_in('administrator');



Loading Controllers - El Forum - 05-28-2009

[eluser]tommymcglynn[/eluser]
Ok, so the line of code that actually redirects the user to a specific controller looks like what?


Loading Controllers - El Forum - 05-28-2009

[eluser]jedd[/eluser]
Code:
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.


Loading Controllers - El Forum - 05-28-2009

[eluser]tommymcglynn[/eluser]
Looks good but I don't understand where the redirect method exists.

I get the following error:

Fatal error: Call to undefined method redirect()


***EDIT***
Nevermind, I needed to load the URL helper.


Loading Controllers - El Forum - 05-28-2009

[eluser]jedd[/eluser]
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.