PHP MVC Framework Tutorial

This post explains you about mvc framework.

MVC stands for Model View Controller.

The model view controller pattern is the most used pattern for web applications.

M (Model)-: Related With Database Query

V (View):- Related With Templates

C (Controller):- Used for communicating Model and View. A place where we write bushiness logic of our program.

MVC Flow:- Firstly user intersect  with controller. Now controller will call to the appropriate model and model return the data and now controller  pass this data   to appropriate view and flow goes again and again.

So here is a simple source code example for PHP MVC framework.


Download Link   Demo Link


Steps for using this framework:

  • Go to under library folder and define all the paths or for using the default path just paste mvc folder into you root directory.
  • Go to product.php under model folder and change you database setting.
  • And just copy sample database attached with zip file and load it into your database.
  • If you wana create a new controller and just create a new php file and put in undercontroller folder  and remember one thing controller file  name and your controller class will be same and additionally Controller  sufix add on it. Like in this example controller name is product and its class name is productController. Same thing apply in case of model.

Framework  Flow:- 

In the first time when user visit the site,  url is:  and when user click to more link url will be

Working of index.php:- index.php is the  entry point of the application, and call everytime whenever user navigate within the website. So firstly we check  that there is any controller call or not, and we check this from query string, so if  $_GET[‘controller’] is set and $_GET[‘controller’] is not empty only than we take its value other wise we call the default controller that is product in our case.

Same thing work in case of function. Firstly we check that there is any function  call or not, and we check this from query string, so if  $_GET[‘function’] is set and $_GET[‘function’] is not empty only than we take its value other wise call our default function that is listing in our case.

And now if  corresponding controller and function not available  in respective folder and file. application will stop using die statement.

And if all is working fine you can create controller object  and call to its function. Now flow goes to controller.

if(isset($_GET['controller']) && !empty($_GET['controller'])){
     $controller =$_GET['controller'];
     $controller ='product'; //default controller

if(isset($_GET['function']) && !empty($_GET['function'])){
    $function =$_GET['function'];
    $function ='listing'; //default function

$fn = SITE_ROOT.'controller/'.$controller . '.php';
       die($function .' function not found');
    $obj=new $controllerClass;
    $obj-> $function();
    die($controller .' controller not found');


Controller Working (product.php):-

loadView used for loading the view and loadModel is used for loading the model, both the function are define in file

 class productController{
   function __construct() {

 function listing(){

 function detail(){

And now welcome to MVC family.

By Rz Rasel Posted in Php

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s