I am a codementor
Search Your Domain Name Here

Simple CRUD example of MVC coding in PHP

Today I have developed a simple example in PHP to explain the concept of MVC (Model, View, Controller) coding. For this purpose I have developed an application which provides the facility of CRUD (Create, Read, Update, Delete) operations on books. So using this application, a new book can be added, all existing books can be listed, an existing book can be edited as well as deleted. And all these operations can be performed only after logging as a user. This application also provides the facility to create a user account, login to that user account as well as facility to reset the password of a user account if it is lost. So all basic facilities of a web application are present in this coding. The demo of this application can be viewed at http://glug4muz.org/demo/bms/. Sample username/password of a user are demo/demo. The code can be downloaded from http://glug4muz.org/demo/bms.zip. The structure of the code is very simple so that any one having basic knowledge of OOP in PHP can easily understand the concept of MVC. All interaction of the application with the user are done using controller. So the main index.php script of the application contains only one object of the controller class and on the basis of the page requested by the user, corresponding method of that object is called. The code of the index.php script is as follows:

<?php
include_once(“controller/controller.php”);

$controller = new Controller();

if(empty($_REQUEST[“page”])) $page=”index”;
else $page=$_REQUEST[“page”];

$controller->$page();
?>

The last line of the above script makes use of the fact that in PHP the name of a function can also be handled using the value of a variable. This is very similar to the concept of variable variable ($$). We can say it is a concept of variable function.

So as soon as a request for a page is received by this script, corresponding method of Controller class is called. The Controller class establishes communication between Model and View. So Controller class is having an object of Model class in it. The code of the Controller class is very simple as given below:

<?php
include_once(“model/model.php”);

class Controller
{
protected $model;

public function __construct()
{
$this->model = new Model();
}

public function index()
{
include_once “view/index.php”;
}

public function register()
{
$status=$this->model->register($errors);
include_once “view/register.php”;
}

public function login()
{
$status=$this->model->login($errors);
include_once “view/login.php”;
}

public function forgot_password()
{
$status=$this->model->forgot_password($errors);
include_once “view/forgot_password.php”;
}

public function home()
{
$logged_in=$this->model->logged_in();
include_once “view/home.php”;
}

public function logout()
{
$logged_in=$this->model->logout();
}

public function profile()
{
$logged_in=$this->model->logged_in();
$status=$this->model->profile($errors, $profile);
include_once “view/profile.php”;
}

public function book_add()
{
$logged_in=$this->model->logged_in();
$status=$this->model->book_add($errors);
include_once “view/book_add.php”;
}

public function book_list()
{
$logged_in=$this->model->logged_in();
$books=$this->model->book_list();
include_once “view/book_list.php”;
}

public function book_edit()
{
$logged_in=$this->model->logged_in();
$status=$this->model->book_edit($errors, $book);
include_once “view/book_edit.php”;
}

public function book_delete()
{
$logged_in=$this->model->logged_in();
$status=$this->model->book_delete($errors, $book);
include_once “view/book_delete.php”;
}

}

?>

As we can see for every possible page, this class contains a method. And these methods are extremely simple. They are simply passing the value received from index.php to a method of Model class object ($this->model) and then the value received from the method of Model class object is being passed to the View script. So the output of the View script will become the output of the corresponding page. It is only that.

For example, suppose when we click on Register menu then the register method of Controller class will be called whose code is as follows:

public function register()
{
$status=$this->model->register($errors);
include_once “view/register.php”;
}

In this function, the first line contains call to the register function of Model class and the value returned is stored in the $status variable. The $errors array is being passed as reference to the register method of Model class and so this array will also be populated with error values if any from the register method of Model class. Then the register.php script of View is being included. So we can make use of the variable $status and the array $errors in the script register.php. The actual logic to register a user account is written in the register method of Model class and the code to display the registration form along with error/success message is written in the View script register.php. So the business logic and appearance of the page are separated from each other. This is the main benefit of this type of coding. In this way both aspects of a page that is its appearance and its business logic can be implemented parallel for fast development of the project. The coder responsible for developing View scripts should have sound knowledge of HTML, JavaScript, CSS and basic knowledge of PHP (if-else and looping statements). The coder responsible for Model class development should have sound knowledge of database and PHP.

The implementation of concept of Model can be done using a single class or multiple classes. Since there exists different types of entities in the system and so separate class should be declared for each entity and then these classes can be integrated with the main Model class. For example, there should be a class for database connectivity, another class for user management and other classes as per requirement. For example in this example, in addition to Database and User classes, a separate class has been defined for Book. All operations related to User are implemented in User class and all operations related to Book are implemented in Book class. I am ready to clarify any confusion about code of this class in the comment of this blog.

Leave a Reply

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

Facebook Auto Publish Powered By : XYZScripts.com