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

[eluser]Unknown[/eluser]
Hi,
I have tried lots of time to understand MVC in CodeIgniter but I failed...

I just want to design a simple page in which view part contains 4 fields Named First Name,Last Name,Email etc...

And I want to check in controller that Email should be valid and all the fields must contain some value...

And I want store it in a database and want to check that Email should be Unique...

May be I can do controller but I dont Understand Model...

So any one can help me what should I do..???

Thanks in Advance..
#2

[eluser]pickupman[/eluser]
Welcome to CI and the forums!

MVC is a common place for a lot of various programming languages. It is a practice of separating: accessing information from a database ([M]odel), what is seen in the browser ([V]iew), and how the logic of those two get are put together ([C]ontroller).

Take a look at the user guide about [url="http://ellislab.com/codeigniter/user-guide/overview/appflow.html"]Application Flow[/url] is good visual diagram. One of the strengths, and weakness some argue about CI, is that it doesn't force you to use strict MVC design. Your controller could have database calls, and html echoed. Your views could access the database, and do some logic.

For something you are designing, I don't think a model is quite necessary. Maybe I am just lazy, but if I only need to add something to the db, I may just do it in the controller. You will want to use the [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html"]form validation library[/url] to check the input of your forms. You would end up with something like
Code:
$this->form_validation->set_rules('firstname', 'first name', 'trim|required|min_length[2]');
$this->form_validation->set_rules('lastname', 'last name', 'trim|required|min_length[2]');
$this->form_validation->set_rules('email', 'email address', 'trim|required|valid_email');

if($this->form_validation->run()){
  //Process form input into DB here
}

//Call view to display to the user
$this->load->view('your_view_form');

With those three simple lines along with the other information about form validation, you can make sure that all 3 fields are required, trim whitespace off, make sure the name fields have at least 2 characters, and a valid email address is given.
#3

[eluser]Unknown[/eluser]
Thanks for the reply…

I am developing that application to learn MVC in CodeIgniter. As I have learned little bit PHP before sometimes and also MVC in Java(J2EE).

I know what is MVC. But how it works in CodeIgniter that I can’t Understand. And Ya that flow was much similar to Java’s MVC.

If I am not wrong then first of all controller will be run. And then it load the View. But using which function (or code) View is loaded and Same for Model (Specially No Idea how model works).
Can you explain me that using which functions (or code) and how that flow is working..??

And also I want to tell one thing that CodeIgniter is a nice framework. I haven’t used any framework before this in PHP. I have seen that it provides so many things which may take a lot of time to develop in PHP without any framework.

Thanks a lot…
#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.




Theme © iAndrew 2016 - Forum software by © MyBB