PHP MVC Framework: A Basic Introduction

php mvc framework tutorial

The Model View Controller pattern is the latest application development framework for developing desktop, mobile, and web applications. It is extremely useful in large scale applications because in MVC, different parts (views, business logic and storage) of a system are developed separately resulting in loosely coupled modular application. As a result, maintenance, flexibility, and scalability of a system increases and code becomes shorter and easier to read. A lot of PHP frameworks are based on MVC patterns which already exist, but even using these solutions require ample knowledge of PHP MVC Framework basics.

What is PHP MVC Framework?

In PHP MVC Framework, the ‘html’ views, business logics controllers and data handling models are developed separately, enabling the designers to work with the views without any concern with the data and its handling; same goes for a programmer whose main focus is on developing the model. This makes PHP application components loosely coupled.

To learn more about PHP, take a course at Udemy.com.

How to implement PHP MVC Framework?

Implementation of the PHP MVC Framework requires understanding of the terms ‘model’, ‘view’ and ‘controller’. Therefore, the following sections demonstrate the detailed description of ‘model’, ‘view’ and ‘controller’ with the example of ‘Book Selection’ application in which a user selects an entry of book from a combo-box and selecting the value, which results in a detailed description of the book. For simplicity, code is maintained in one file.

Model in PHP MVC Framework

In PHP MVC Framework, the model is responsible for managing the data that involves the storage and retrieval of entities like the ‘user’ or a ‘product order’ from the database or stored arrays. The model is a part of application that involves processing the data required by an application. The model part is unaware of the business logic and its corresponding view.

In the example of  a‘Book Selection’ model, part of PHP MVC framework is represented by two classes ‘Book’ and ‘Model’. The ‘Book’ is an entity class used to keep the book’s data. The ‘model’ class is used to store and retrieve the book’s data.

Code snippet (Book Class):

<?php

classBook {

public $name;

public $version;

public $yearOfPublish;

public function __construct ( $name, $version, $yearOfPublish )

{

$this -> name = $name;

$this -> version = $version;

$this -> yearOfPublish = $yearOfPublish;

}

}

?>

Code snippet (Model Class):

<?php

classModel {

public $text;

public function __construct ()

{

$this -> text = 'Book Selection';

}

public function getBookList ()

{

return array (

'PHP for Absolute Beginners' => new Book ('PHP for Absolute Beginners', '1.2', '1999'),

'Basic Introduction to PHP MVC' => new Book ('Basic Introduction to PHP MVC',

'1.0', '2008'),

'Advanced PHP' => new Book ('Advanced PHP', '1.2', '2004'),

'Advanced PHP MVC' => new Book ('Advanced PHP MVC', '2.0', '2014')

);

}

public function getBook ($name)

{

$allBooks = $this -> getBookList ();

return $allBooks [$name];

}

}

?>

In the above code snippets of PHP MVC framework example, the ‘Book’ class is used to keep the data of a book’s name, version and publishing year. The ‘model’ class stores the book’s data in the form of associative array with a method to return all the books data and a single book filtered by the ‘name’ attribute with addition of the ‘text’ property that has the application name.

Controller in PHP MVC Framework

In PHP MVC framework, the controller takes an input from user as a request, processes and analyzes this input, initializes and invokes the model, takes the model response and sends it back to the respected view. So controller is a part of application that involves the logical processes of the application also called the business logic. It acts as a middle layer for views and models.

In the example of a ‘Book Selection’ controller, part of PHP MVC framework is represented by one class ‘Controller’. This class instantiates the model class and call the methods to retrieve the book’s information based on the request nature.

Code snippet (controller class):

<?php

classController {

private $model;

public function __construct (Model $model)

{

$this -> model = $model;

}

public function invoke ()

{

if (!isset ($_GET['book']))

{

return $this -> model -> getBookList ();

}

else

{

return $this -> model -> getBook ($_GET['book']);

}

}

public function output ()

{

return'<h1>' . $this -> model -> text . '</h1>';

}

}

?>

In the above code snippet of PHP MVC framework example, the ‘controller’ class instantiates the ‘model’ using its constructor. Different methods of ‘model’ are called based on the request nature. When the page is loaded for the first time, a ‘getBookList’ method of ‘model’ is called to return all book details and ‘getBook’ is called when a ‘GET’ request is made, which results in a specific book detail based on the requested book name. The ‘output’ function of a controller is called to get the application name saved in ‘text’ property of ‘model’ class.

Interested in studying more about PHP? Check out a course at Udemy.com.

View in PHP MVC Framework

In PHP MVC framework, the view is responsible for all the display logic used to present data from the ‘model’ to the user. It passes the user input to a controller using control events. In short, a view is a part of application that involves the generation of the html and interaction with user.

In the example of ‘Book Selection’, the view presents data to a user with a combo-box and a submit button. It instantiates the ‘Model’ and ‘Controller’ classes to call the ‘invoke’ method of a controller and populates the combo-box with the retrieved data.

Code snippet:

<?php

$model = new Model ();

$controller = new Controller ($model);

echo $controller -> output ();

$books= $controller -> invoke ();

if ($_SERVER['REQUEST_METHOD'] === "GET")

{

if (isset ($_GET['book']))

{

echo "You selected " . $books -> name . " version: " . $books -> version . "

Year of published: " . $books -> yearOfPublish ;

exit;

}

}

?>
 

<form method="GET" action="" >

<select name="book" >

<?php

foreach ($books as $x => $x_value)

{

echo '<option value="'.$x.'">'.$x.'</option>';

}

?>

</select>

<input type="submit" value="Select">

</form>

In the above code snippet, the ‘output’ method of controller is called to generate the name of the application with the html heading tag. Next,the ‘invoke’ method is called to retrieve the book’s information. A combo-box is filled with the returned book names. When you click the ‘submit’ button, a request is created with a book name selected in a combo-box to the same view, which displays details of selected book as output HTML page.

For more interesting PHP tutorials, check out courses offered by Udemy.com.