PHP Codeigniter MVC Concept For Dummies With Simple Example

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

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.

Codeigniter Mvc Concept

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:

Then open your browser please access URL http://localhost/mycodeigniter/index.php/product

Learn Controller Codeigniter Mvc Concept For Beginner

You can also access it in the following way http://localhost/mycodeigniter/index.php/product/index.

Add the following script to the above controller

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.

Passing Parameter To Controller Method Codeigniter With Uri Segment

To get the above results, please add the following script in the edit method.

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

Change the index() method of the Product.php controller to load the product_list.php view with the following code

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

Please edit product_list.php views and add the following code

see on your browser http://localhost/mycodeigniter/index.php/product/

Views Codeigniter

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

To execute the model into the controller use the following code

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.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.