Passing data between controllers? Calling a controller from another controller. |
[eluser]Unknown[/eluser]
Hi, everyone. I'm in a bit of a pickle here... dunno if there is some structural problem with what I want to do, but I've been unable to find documentation about this. I have a login controller, that after doing what it is supposed to do, reaches its fundamental decision: To log in, or not to log in. Code: if ($this->validation->run()) If it doesn't, it just loads the login view, but if it does, I want the main controller to take charge, and I want to pass some data to it (the logged user, for example). It would be something (that doesn't exist, as far as I know) like: Code: $this->load->controller('main',$data); Any ideas on this? Thanks in advance for your kind help!
[eluser]ejangi[/eluser]
Loading a controller from a controller would be messy I imagine, as there's no built-in way to do it. But, it's because you usually wouldn't go down that route. Why do you need to load the controller directly? Why not store your login information in a DB or Session and then just redirect to the controller?
[eluser]Michael Ekoka[/eluser]
Some quick suggestions, feel free to beef them up a bit. 1-) If you absolutely want to pass variables to your controllers from previous requests: Create a base controller that will be extended by all your other controllers. Add to it some kind of archive variable. The principle is simple. Upon initialization, the controller copies data from the $_SESSION to its archive variable and then clear the $_SESSION archive copy to prepare it for the current request. Code: class MY_Controller extends Controller{ Now simply store values using the _next() method from your controller and they will be available in your next request. 2-) Second suggestion: If your problem is just for the login. You can create a Login controller as you did: Code: class Login extends Controller{ Code: class MY_Controller extends Controller{ Code: class Some_controller extends MY_Controller{
[eluser]Unknown[/eluser]
[quote author="ucantblamem" date="1198034218"]Loading a controller from a controller would be messy I imagine, as there's no built-in way to do it. But, it's because you usually wouldn't go down that route. Why do you need to load the controller directly? Why not store your login information in a DB or Session and then just redirect to the controller?[/quote] Mhh, probably I'm doing something the wrong way here, some old way to do it.. dunno. But my idea was this: * A controller associated with a view for the Login process, called Login * A controller associated with a view for the main function of the application, called Main. My idea was: you try to log in. If you are unsuccessful, you're thrown back out, to the Login view again. But if you succeed, you log in, and you let the application know who you're. The log in controller knows who you are, as it let you in; but how does it let it know to the rest of the application, namely the next controller. I can log in, because I've initiated a session, but how do I pass that info to the next controller? is there some way of passing this info to the session "variable", like you would with a cookie? I may be doing something fundamentally wrong here :-S
[eluser]ejangi[/eluser]
The idea of a "session" is to store information about that user's "session" in your application, so it's absolutely the best place to store this stuff (that's why sessions exist really). The CodeIgniter user-guide does a pretty good explanation of sessions, what they are and how to use them. What I would be doing in your case is, if the user is successful, store their user_id and/or username with: Code: $this->session->set_userdata('id' => $user_id, 'username' => $username); Code: $username = $this->session->userdata('username'); Likewise, if the login fails, set whatever session data you need and then redirect them back to your login page. Hope this helps! |
Welcome Guest, Not a member yet? Register Sign In |