Model, View and Controller in CodeIgniter

Model, View and Controller in CodeIgniter

In this post i will explain about MVC(Model, View, Controller), CodeIgniter is based on the Model, View and Controller pattern.

MVC is a software approach that divide application logic from presentation. It allow that your web pages to contains minimum code since the presentation is divide from the PHP code.

      MVC stands for Model, View and Controller
  • Controller basically a class, it perform action, Controller communicate between model and view.
Controller control whole application. URL hits with controller. To connect every think model , view and URL called Controller.

Controller handled business logic, calculation, form validation etc. Default controller is welcome.php and CI_Controller is a base class. Every new controller extends CI_Controller.

Naming convention of Controller
File name and controller name are same but first letter of controller name should be capital for example welcome.php is file name and Welcome is controller name.

Create Controller
Suppose your root folder of CodeIgniter is CITEST and xampp is installed on C drive, then go to this path C:\xampp\htdocs\CITest\application\controllers and create controller.

Now I will create a new controller whose name is blog.php and function inside blog controller is add(), code shown below.

<?php

     defined('BASEPATH') OR exit('No direct script access allowed');

     class Blog extends CI_Controller {

public function add()
{
echo "Hello Digital World....";
}


     }

I will call add() function, go to browser and type localhost/CITEST/index.php/blog it gives error screen shown below because by default index method is called but index method is not present


Model, View and Controller in CodeIgniter

Now I will add index function inside blog controller, code shown below.

<?php
    
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Blog extends CI_Controller {

public function index()
{
       echo "Hello Digital World";
}
        
        Public function index()
        {
              echo "Hello World"; 
        }
     }

then go to browser and type same URL localhost/CITEST/index.php/blog output screen shown below.


Model, View and Controller in CodeIgniter

because by default index method is called automatically.

Now I will call add() function and go to browser and type localhost/CITEST/index.php/blog/add screen shown below.

Model, View and Controller in CodeIgniter

  • Model, is a place where logic, database connectivity etc it simply deals backend part like Database, APIs ets. Generally mysql database used for backend part.
Model contains function that helps to insert, select and update etc data in database. 
Every new Model extends CI_Model

Naming Convention of Model
File name and model name both are same but first letter of model name should be capital for example blog_model.php is file name and Blog_model is model name.

Create Model
Suppose your root folder of CodeIgniter is CITEST and xampp is installed on C drive, then go to this path C:\xampp\htdocs\CITest\application\models. I will create a model, code shown below.

<?php

Class Blog_model extends CI_Model{

public function show()
{
echo "Welcome.....";
}
}


?>

How to use Model
If you want to use Model, first load that Model in Controller and call the function inside that Model, code shown below

      $this->load->model('blog_model');
      $this->blog_model->show();

Here blog_model is name of Model and show() function inside blog_model Model.

Now go to browser and type localhost/CITEST/index.php/blog or localhost/CITEST/index.php/blog/index screen shown below


Model, View and Controller in CodeIgniter

Note- Suppose your model name is very big like facebooksocialmedia_model, and want to add this model in controller, in this case use second parameter of model.

Create Model

<?php

Class Socialmediafacebook_model extends CI_Model{

public function show()
{
echo "Welcome.....";
}
}


?>

Load Model in blog Controller and call show function

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Controller {

public function index()
{
$this->load->model('socialmediafacebook_model', 'fb');
                        &this->fb->show();
}

      }
  • View is a place where user views html pages, it is a presentation part like html, javascriptetc. It is front end part for application.
Views normally a web pages, but in CodeIgniter a view can also a page fragment like a header and footer.

Create View
Suppose your root folder of CodeIgniter is CITEST and xampp is installed on C drive, then go to this path C:\xampp\htdocs\CITest\application\views and create view whose ame is blog_message.php, code shown below

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Blog</title>
<link rel="stylesheet" href="">
</head>
<body>
<h1>This is my First Blog...........</h1>
</body>

</html>

How to Use View
First load view in blog controller, code shown below

       $this->load->view('blog_message');

go to this path C:\xampp\htdocs\CITest\application\controllers and load the view, code shown below

<?php

defined('BASEPATH') OR exit('No direct script access allowed');


class Blog extends CI_Controller {

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


       }

Note- No need to give the file extension when you load the view in controller simply give the view name.

Now go to browser and type localhost/CITEST/index.php/blog or localhost/CITEST/index.php/blog/index screen shown below


Model, View and Controller in CodeIgniter


                 I hope, it helped you to understand the Model, View and Controller in CodeIgniter.......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...