Welcome Guest, Not a member yet? Register   Sign In
Cant understand How MVC works in CodeIgniter...
#4

[eluser]pickupman[/eluser]
As shown in the Application Flow Diagram, the first thing run is the call to the Router, to tell what CI to do. It will tell which controller needs to execute. Controllers are what executed and controls what goes on. You would use them to process any data, make calls to models for information, and then call a view to render to the browser.

For your example you would need a controller name something like contact.php. Controllers are classes so they work like object oriented programming style coding. Controller class names are the first segment of your url, and the second segment is the method to run in the class.

A view can be name anything. Usually best practice is to put them in subfolders named after your controller and name the files after the method they are for. Then at the end of a method in your controller you load the view to be displayed.
/views
/contact
/index.php
/thank_you.php

A model is a easy way to get/edit/delete information in a database. You don't have to use models, but in coding don't repeat yourself. By make calls to the database in a model, you can use the same code in a controller to get information from the database without having to keep making query strings. Example:
Code:
//No model
$query = $this->db->select('*')->from('mytable')->where('id', $id);

//Model called contact_model
function get_mytable($id){
  return $this->db->select('*')->from('mytable')->where('id', $id);
}
//Controller with loaded model
$query = $this->contact_model->get_mytable($id);

Now you could call that query many times with fewer code. And if you ever decide you need to call a different table, or change a where statement, the code will be all in one file rather than searching through controllers.


Messages In This Thread
Cant understand How MVC works in CodeIgniter... - by El Forum - 04-07-2011, 06:40 AM
Cant understand How MVC works in CodeIgniter... - by El Forum - 04-07-2011, 07:04 AM
Cant understand How MVC works in CodeIgniter... - by El Forum - 04-07-2011, 09:43 AM
Cant understand How MVC works in CodeIgniter... - by El Forum - 04-07-2011, 10:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB