Reputation: 92581
I am new to Code Igniter.
I am looking to re-write our current system to use code igniter.
My current question is.
I am transferring the login form. Currently we have a page with receives an ajax request and validates the data and returns the status when the form is submitted.
How should I go about this in Code Igniter? My thoughts are to create a controller which receives the request.
But,
A controller should not spit out any response.
So does that mean I should create a view just to spit out a couple of lines of json?
Upvotes: 0
Views: 314
Reputation: 422
Here is an example of how you might be able to do the login. This is from one of my own sites. Would also love feedback from others if you see best practices that I am missing
VIEW
if (isset($message)) {echo $message;} //error message
echo form_open('login/validate_credentials', 'class="form-container"');
echo form_input('username', 'Username', 'class="form-field"');
echo form_password('password', 'Password', 'class="form-field"');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Need an account? Enroll now!');
echo form_close();
CONTROLLER
function validate_credentials()
{
$this->load->model('usermodel');
$query = $this->usermodel->validateUser();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true,
'bt_link' => $this->usermodel->getBTlink($this->input->post('username')),
);
$this->session->set_userdata($data); //store in session
$this->load->view('membership'); //logged in
}
else // incorrect username or password
{
$data['message'] = "Invalid credentials";
$data['header_type'] = 'header';
$data['main_content'] = 'login_form';
$this->load->view('template', $data);
}
}
Here is a great CI tutorial
http://net.tutsplus.com/sessions/codeigniter-from-scratch
Upvotes: 0
Reputation: 4923
Code Igniter has an input class and a form validation class. They both have methods that are helpful for handling form input:
I would create an account controller with a login method. The login method would use the previously mentioned classes for form handling and then call your model to execute the DB query.
I don't think having an ajax controller would be of much value to you. You'll likely end up using ajax in all of your controllers. If you want to think about optimizing your controllers for ajax, I would break up all of the "moving parts" into small loosely coupled tasks. Since ajax requests don't reload the whole page it makes it easier to run only the required logic.
On the front end, it's helpful to use template-based layouts. This way the only thing you send to the output is portion being updated. The Code Igniter parser class helps with templating too.
Upvotes: 1
Reputation: 526623
Sure. You could even make it a generic view that can spit out any JSON you want, and then pass in the object to be json_encode
d to the view. That way you could reuse the same view for other controllers that handled AJAX requests as well.
Upvotes: 1