• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » PHP Codeigniter MVC Concept For Dummies With Simple Example

PHP Codeigniter MVC Concept For Dummies With Simple Example

By Sigit Prasetya Nugroho ∙ July 1, 2017 ∙ PHP ∙ 1 Comment

Share : TwitterFacebookTelegramWhatsapp

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

  • 1 MVC (Model, View, Controller)
    • 1.1 Three types of components build an MVC pattern in an application that is:
    • 1.2 Passing parameter to controller method with URI Segment
    • 1.3 Passing data from controller to views in Codeigniter

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:

Related Articles :

  • CodeIgniter Tutorial How To Make Rest API ( Server And Client )
  • Simple CRUD Apps With Codeigniter Tutorial For Beginners
  • How To Remove Index php Url Path Codeigniter

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

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

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.

Passing Parameter To Controller Method Codeigniter With Uri Segment

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/

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

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.

Another PHP Related Post :

  • Tutorial Create Simple POS Using ReactJS And Laravel Lumen Part 1
  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for RickyRicky says

    March 29, 2018 at 8:14 am

    Nice tutorial

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for RickyThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (6) Reactjs (9) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2022 Seegatesite.com