views:

1111

answers:

2

I'm looking at the demo videos at the CodeIgniter site and browsing through the documentation but its is unclear to my how I can achieve dynamic navigation from one page to another, depending on user input.

For example I'd like to have a login form that will either forward to a "success page" or a "login failed page".

Where should I put this functionality?

A: 

Generally in a MVC framework such as Codeigniter, the logic to preform dynamic navigation would reside in the controller.

Example: (in php flavored pseudo-code)

<?php
class Blog extends Controller {

    function login($username, $password)
    {
     if ($username and $password are correct) {
                    $this->load->view('success');
                    return;
                }

     $this->load->view('fail', $data);
    }
}
?>

I don't actually use Codeigniter or PHP but I have MVC experience in other languages. Also due to my inexperience in the language/framework please do not use the above code... It was only an example. :D

Carl Sverre
+3  A: 

Okay, so for this login example you'll need the the Form Helper, the Form Validation Class and the URL Helper.

class Login extends MY_controller {

function index()
{
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');

    // some simple rules
    $this->form_validation->set_rules('username', 'Username', 'required|alpha_dash|max_length[30]');
    $this->form_validation->set_rules('password', 'Password', 'required');
    if ($this->form_validation->run() == FALSE) {
        // This will be the default when the hit this controller/action, or if they submit the form and it does not validate against the rules set above.
        // Build your form here
        // Send it to a login view or something
    } else {
        // The form has been submitted, and validated
        // At this point you authenticate the user!!
        if ($userIsAuthenticated) {
            redirect('controller/action'); //anywhere you want them to go!
        } else {
            // not authenticated...in this case show the login view with "bad username/password" message
        }
    }
}

tl;dr

Use the redirect() fuction in the URL Helper to send the user to other pages based on logic in your controller.

mrinject