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 common.inc.php 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:  http://www.91weblessons.com/demo/mvc  and when user click to more link url will be http://www.91weblessons.com/demo/mvc?controller=product&function=detail&id=1

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.

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'library/common.inc.php');
if(isset($_GET['controller']) && !empty($_GET['controller'])){
     $controller =$_GET['controller'];
}else{
     $controller ='product'; //default controller
}

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

$controller=strtolower($controller);
$fn = SITE_ROOT.'controller/'.$controller . '.php';
if(file_exists($fn)){
    require_once($fn);
    $controllerClass=$controller.'Controller';
    if(!method_exists($controllerClass,$function)){
       die($function .' function not found');
    }
    $obj=new $controllerClass;
    $obj-> $function();
}else{
    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 common.inc.php file

<?php
 class productController{
   function __construct() {
 }

 function listing(){
    loadView('header.php');
    $arrValue=loadModel('product','showProductListing');
    loadView('product_listing.php',$arrValue);
    loadView('footer.php');
 }

 function detail(){
    loadView('header.php');
    $id=$_GET['id'];
    $arrArgument=array('id'=>$id);
    $arrValue=loadModel('product','productDetails',$arrArgument);
    loadView('product_detail.php',$arrValue);
    loadView('footer.php');
 }
}
?>

And now welcome to MVC family.

Advertisements
By Rz Rasel Posted in Php

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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