What is MVC (Models Views and Controllers) in codeigniter framework?

MVC (Models, views, Controllers)

A controller is a simple class file. it controls the whole application by URI.

Creating a Controller :-

First, go to application/controllers folder. You will find two files there, index.html and Welcome.php. These files come with the CodeIgniter. Keep these files as they are. Create a new file under the same path named "Mytest.php". Write the following code in that file −

File name : index.php

<?php
class Ittutorial extends CI_Controller {

public function index() {
echo "Hello Sana!";
}
}
?>

The Test class extends an in-built class called CI_Controller. This class must be extended whenever you want to make your own Controller class.


Calling a Controller :-

The above controller can be called by URI as follows −
http://www.ittutorial.in/index.php/test


Notice the word “test” in the above URI after index.php. This indicates the class name of controller. As we have given the name of the controller “Test”, we are writing “test” after the index.php. The class name must start with uppercase letter but we need to write lowercase letter when we call that controller by URI. The general syntax for calling the controller is as follows −

http://www.ittutorial.in/index.php/controller/method-name


How to calling a method.

File name : index.php

<?php
class Ittutorial extends CI_Controller {

public function index() {
echo "This is default function.";
}

public function hello() {
echo "This is hello function.";
}
}
?>

We can execute the above controller in the following three ways −

  • http://www.ittutorial.in/index.php/test
  • http://www.ittutorial.in/index.php/test/index
  • http://www.ittutorial.in/index.php/test/hello
  • Points to Remember :

  • The name of the controller class must start with an uppercase letter.
  • The controller must be called with lowercase letter.
  • Do not use the same name of the method as your parent class, as it will override parent class’s functionality.

  • view

    view is a webpage, which can be called by the controller. View cannot be called directly. Create a new file under application/views with name mytest.php and copy the below given code in that file.

    A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.

    Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.

    File name : index.php

    <!DOCTYPE html>
    <html lang = "en">

    <head>
    <meta charset = "utf-8">
    <title>CodeIgniter View Example</title>
    </head>

    <body>
    CodeIgniter View Example
    </body>

    </html>


    Loading the View

    The view can be loaded by the following syntax −

    $this->load->view('name');
    Where name is the view file, which is being rendered. If you have planned to store the view file in some directory then you can use the following syntax −

    Storing Views within Sub-directories


    $this->load->view('directory-name/name');
    It is not necessary to specify the extension as .php, unless something other than .php is used.


    The index() method is calling the view method and passing the “test” as argument to view() method because we have stored the html coding in “mytest.php” file under application/views/mytest.php.

    <?php
    class Test extends CI_Controller {

    public function index() {
    $this->load->view('mytest');
    }
    }
    ?>

    Adding Dynamic Data to the View

    Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:

    File name :

    $data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message'
    );

    $this->load->view('blogview', $data);

    And here’s an example using an object:

    $data = new Someclass();
    $this->load->view('blogview', $data);
    <?php
    class Blog extends CI_Controller {

    public function index()
    {
    $data['title'] = "My Real Title";
    $data['heading'] = "My Real Heading";

    $this->load->view('blogview', $data);
    }
    }

    Now open your view file and change the text to variables that correspond to the array keys in your data:

    <html>
    <head>
    <title><?php echo $title;?></title>
    </head>
    <body>
    <h1><?php echo $heading;?></h1>
    </body>
    </html>

    Creating Loops

    The data array you pass to your view files is not limited to simple variables. You can pass multi dimensional arrays, which can be looped to generate multiple rows. For example, if you pull data from your database it will typically be in the form of a multi-dimensional array. Here’s a simple example. Add this to your controller:

    <?php
    class Blog extends CI_Controller {

    public function index()
    {
    $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');

    $data['title'] = "My Real Title";
    $data['heading'] = "My Real Heading";

    $this->load->view('blogview', $data);
    }
    }
    Now open your view file and create a loop:

    <html>
    <head>
    <title><?php echo $title;?></title>
    </head>
    <body>
    <h1><?php echo $heading;?></h1>

    <h3>My Todo List</h3>

    <ul>
    <?php foreach ($todo_list as $item):?>

    <li><?php echo $item;?></li>

    <?php endforeach;?>
    </ul>

    </body>
    </html>


    Models :-

    Models classes are designed to work with information in the database. As an example, if you are using CodeIgniter to manage users in your application then you must have model class, which contains functions to insert, delete, update and retrieve your users’ data.

    How to create a Model Class :-

    Model classes are stored in application/models directory.

    File name : Auth_model.php

    <?php
    Class Auth_model extends CI_Model {

    Public function __construct() {
    parent::__construct();
    }
    }
    ?>

    Where Model_name is the name of the model class that you want to give. Each model class must inherit the CodeIgniter’s CI_Model class. The first letter of the model class must be in capital letter.

    Loading Model

    Model can be called in controller. Following code can be used to load any model.

    $this->load->model('model_name');
    Where model_name is the name of the model to be loaded. After loading the model you can simply call its method as shown below.

    $this->model_name->method();

    Auto-loading Models

    $autoload['model'] = array();
    // $autoload['model'] = array('first_model', 'second_model');

    File name : index.php


    File name : index.php






    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here