The Basics of MVC Architecture in PHP

1
102

PHP on the sea

MVC, which stands for Model-View-Controller, is a really good way to develop clean, scalable, powerful and fast applications in the least amount of time and with the least effort. Before exploring MVC, this article begins with a brief introduction to PHP.

PHP is an open source programming language that was developed in 1994 by Rasmus Lerdorf. PHP originally stood for Personal Home Pages but now stands for Hypertext Preprocessor, which is a recursive acronym. It is a loosely typed server-side scripting language used to build Web applications. A scripting language is a language that does not need to be compiled before use. And ‘loosely typed’ languages are those in which there is no need to define the type of a variable.
Today, more than 20 million domains use PHP, including Facebook and Yahoo.
PHP is easily embedded with HTML, and is used to manage dynamic content and the databases of websites or, we can say, Web applications. We can use PHP with many popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server.

How PHP works on servers
When a client (browser) sends a request for a PHP page to the server, the server reads the requested file from the disk (storage) and sends this file to the interpreter. The interpreter then runs the PHP code, fetches the DB data (if required) and puts it into HTML tags. Once the interpreter completes its tasks, it sends the result back to the server, which sends this data to the client (browser) that made a request for this page.

MVC architecture with PHP
The Model-View-Controller concept involved in software development evolved in the late 1980s. It’s a software architecture built on the idea that the logic of an application should be separated from its presentation. A system developed on the MVC architecture should allow a front-end developer and a back-end developer to work on the same system without interfering with each other.

Model
Model is the name given to the component that will communicate with the database to manipulate the data. It acts as a bridge between the View component and the Controller component in the overall architecture. It doesn’t matter to the Model component what happens to the data when it is passed to the View or Controller components.
The code snippet for running first_model.php is:

<?php
class Model
{
public $string;
public function __construct()
{
$this->string = “Let’s start php with MVC”;
}
}
?>

View
The View requests for data from the Model component and then its final output is determined. View interacts with the user, and then transfers the user’s reaction to the Controller component to respond accordingly. An example of this is a link generated by the View component, when a user clicks and an action gets triggered in the Controller.
To run first_view.php, type:

<?php
class View
{
private $model;
private $controller;
public function __construct($controller,$model)
{
$this->controller = $controller;
$this->model = $model;
}
public function output()
{
return “<p>” . $this->model->string . “</p>”;
}
}
?>

Controller
The Controller’s job is to handle data that the user inputs or submits through the forms, and then Model updates this accordingly in the database. The Controller is nothing without the user’s interactions, which happen through the View component.
The code snippet for running first_controller.php is:

<?php
class Controller
{
private $model;
public function __construct($model)
{
$this->model = $model;
}
}
?>

A simple way to understand how MVC works is
given below.
1) A user interacts with View.
2) The Controller handles the user input, and sends the information to the model.
3) Then the Model receives the information and manipulates it (either saving it or updating it by communicating with the database).
4) The View checks the state of the Model and responds accordingly (lists updated information).
In the following code snippet, by running first_example.php we can see our MVC architecture work with PHP.
To run first_example.php, you must type:

<?php
$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);
echo $view->output();
?>

1 COMMENT

  1. i understand and best explanation …but i want to know that we click on edit or delete button so logic in controller so where pass the id suppose in model transfer then please explain

LEAVE A REPLY

Please enter your comment!
Please enter your name here