Welcome Guest, Not a member yet? Register   Sign In
Passing data between controllers? Calling a controller from another controller.
#1

[eluser]Unknown[/eluser]
Hi, everyone. Smile

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())
    { redirect('main');   }
else
    { $this->load->view('login'); }

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

[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?
#3

[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{
    private $archive;

    function __construct(){
        parent::__construct();

        // copy data from previous request then clear the $_SESSION archive variable
        // to prepare it for the current request
        $this->archive = @$_SESSION[PROJECT_ID]['archive'];
        unset(@$_SESSION[PROJECT_ID]['archive']);
    }
    
    function _next($name,$value){
        // store some value from the current request to be retrieved in the next
        $_SESSION[PROJECT_ID]['archive'][$name] = $value;    
    }
}

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{

        // process login
    function index(){
        // if username && password && permission match in the database, set a flag
        $_SESSION[PROJECT_ID]['logged_in'] = true;
    }
}
Now, create a base controller (in application/libraries/MY_Controller.php). In it, simply add in an authenticate() method that checks for the value of the previously set flag.
Code:
class MY_Controller extends Controller{
    function authenticate (){
        if(@$_SESSION[PROJECT_ID]['logged_in'] === true){
            return true;
        }

                // if the flag wasn't set redirect user to the login and explicitly return false
        redirect('login');
        return false;
    }
}
You can now use this authenticate() method from any controller that extends the MY_Controller base class. e.g. in /application/controllers/some_controller.php
Code:
class Some_controller extends MY_Controller{
    function __construct(){
        parent::__construct();

                // if the authenticate() method returns false, explicitly end the script
                // even though the request has already been redirected
        if(!$this->authenticate()){
            exit;
        }
    }
}
This will redirect any unauthenticated requests to the login page.
#4

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

[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);
redirect them to your main controller and then you can retrieve that same information from the session (in your main controller for instance) by doing the following:
Code:
$username = $this->session->userdata('username');
$user_id = $this->session->userdata('id');

Likewise, if the login fails, set whatever session data you need and then redirect them back to your login page.

Hope this helps!




Theme © iAndrew 2016 - Forum software by © MyBB