The CodeIgniter framework MVC Concept has a relatively easy to understand. MVC concept itself aims to facilitate the division of tasks within a team. For example, the programmer handles the model and controller, while the designer takes care of the view. Let us understand the concept of MVC on CodeIgniter with a simple example in the following tutorial.
Read another article: Introduction – PHP Codeigniter Tutorial For Dummies Step By Step
Table of Contents
MVC (Model, View, Controller)
Model View Controller is a relatively familiar concept in web application development, starting with Small Talk programming language, MVC separates application development based on main components that build an application such as data manipulation, user interface, and part of application control.
Three types of components build an MVC pattern in an application that is:
The Controller is a set of communication between the model and view, the controller functions to receive requests and data from the user and then determine what will be processed by the application.
The controller in the CodeIgniter contains a PHP class that is a derivative of the CI_controller class. The creation of the controller file must start with a capital letter. The creation of the controller file is contained within the folder your-CodeIgniter/application/controllers/. For example, create a new controller named Product.php and add the following class:
1 2 3 4 5 6 | <?php class Product extends CI_Controller { public function index(){ echo 'Hello, this is product page'; } } |
Then open your browser please access URL http://localhost/mycodeigniter/index.php/product
You can also access it in the following way http://localhost/mycodeigniter/index.php/product/index.
Add the following script to the above controller
1 2 3 4 5 6 7 8 9 | <?php class Product extends CI_Controller { public function index(){ echo 'Hello, this is product page'; } public function edit(){ echo 'Edit page'; } } |
Then open your browser please access URL http://localhost/mycodeigniter/index.php/product/edit
Passing parameter to controller method with URI Segment
The URI Segment is almost similar to the GET Method variable. To further understand it please refer to the following picture.
To get the above results, please add the following script in the edit method.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php class Product extends CI_Controller { public function index(){ echo 'Hello, this is product page'; } public function edit($id,$name){ echo 'URI Segment 1 : '.$this->uri->segment(1).'<br/>'; echo 'URI Segment 2 : '.$this->uri->segment(2).'<br/>'; echo 'URI Segment 3 : '.$this->uri->segment(3).'<br/>'; echo 'URI Segment 4 : '.$this->uri->segment(4).'<br/>'; } } |
The full controller tutorial can be found at https://www.codeigniter.com/userguide3/general/controllers.html
The View is the part that handles presentation logic. In a web application, this section is usually an HTML template file, which is controlled by the controller. View functions to receive and represent data to the user. This section has no direct access to the model section.
All “view” files in CodeIgniter are located in your-CodeIgniter/application/views folder. To further understand it please create a PHP file with a product_list.php name in the “views” folder. Then add the following code and save it
1 | <h2>Product List</h2> |
Change the index() method of the Product.php controller to load the product_list.php view with the following code
1 2 3 4 5 6 7 8 9 10 11 12 | <?php class Product extends CI_Controller { public function index(){ $this->load->view('product_list'); } public function edit($id,$name){ echo 'URI Segment 1 : '.$this->uri->segment(1).'<br/>'; echo 'URI Segment 2 : '.$this->uri->segment(2).'<br/>'; echo 'URI Segment 3 : '.$this->uri->segment(3).'<br/>'; echo 'URI Segment 4 : '.$this->uri->segment(4).'<br/>'; } } |
Please access the following URL http://localhost/mycodeigniter/index.php/product/ and see the results.
Passing data from controller to views in Codeigniter
We can create array to store data like the example below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php class Product extends CI_Controller { public function index(){ $data['product_list'] = array('snack', 'drink', 'foot'); $data['title'] = "Restaurant Menu"; $this->load->view('product_list',$data); } public function edit($id,$name){ echo 'URI Segment 1 : '.$this->uri->segment(1).'<br/>'; echo 'URI Segment 2 : '.$this->uri->segment(2).'<br/>'; echo 'URI Segment 3 : '.$this->uri->segment(3).'<br/>'; echo 'URI Segment 4 : '.$this->uri->segment(4).'<br/>'; } } |
Please edit product_list.php views and add the following code
1 2 3 4 5 6 7 | <h2>List of <?php echo $title; ?></h2> <hr /> <?php foreach ($product_list as $list) { echo $list.'<br/>'; } ?> |
see on your browser http://localhost/mycodeigniter/index.php/product/
Models usually directly related to the database to manipulate data (insert, update, delete, search), handle validation from the controller section, but can not be directly linked to the view.
All “model” files are put in your-Codeigniter/application/models/
Create a new model named “product_model.php” and add the following script
1 2 3 4 5 6 7 8 | <?php class Product_model extends CI_Model { function list_product(){ $product = array('snack', 'drink', 'foot'); return $product; } } ?> |
To execute the model into the controller use the following code
1 2 3 4 5 6 7 8 9 | <?php class Product extends CI_Controller { public function index(){ $this->load->model('product_model'); $data['product_list'] = $this->product_model->list_product(); $data['title'] = "Restaurant Menu"; $this->load->view('product_list',$data); } } |
Model information on the full codeigniter you can read on the official website https://www.codeigniter.com/userguide3/general/models.html#models
How? To understand the MVC concept of Codeigniter is very easy right? In the next article will create a simple crud application using CodeIgniter and MySQL database.
So my article about Codeigniter MVC Concept For Dummies With Simple Example may be useful.
Nice tutorial