MVC stands for “Model View and Controller“. The main aim of MVC in php is to separate the Business logic & Application data from the USER interface. So in this tutorial we will learn about PHP MVC Framework.

MVC Architecture in PHP

The Model is responsible to manage the data because it stores and retrieves entities used by an application, usually from a database, and contains the logic such as fetch & update data etc. implemented by the application.

The View is responsible to display the data provided by the model. End user GUI through which user can interact with application i.e. HTML, CSS.

Controller receives a request from the user, invokes the model to perform the requested operations and sends the data to the view. It contain business logic and provide a link between model and view. The controller mediates between the models and views.

Why use PHP MVC Framework?

PHP MVC Framework helps to remove complexity.

It provides standard methods for building our applications.

Increased developer productivity, this is because the base implementation of activities.

Adherence to professional coding standards.

Learn to create login page in php

PHP example has a simple structure, putting each MVC module in one folder. There are three folder “controller”, “model”, “view”.

1.) In the controller, we have created the UserController php file.

2.) Inside the model, we have created the User php file.

3.) Inside the view, we have created the userlist & viewuser php file.

PHP MVC Framework

The Controller is responsible for taking a request, parses it, initializes and invoke the model and takes the response given by model and sends it to the view section. 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 application entry point will be index.php. The index php file will delegate all the requests to the controller.

File Name: index.php

<?php include_once("controller/UserController.php"); $controller = new UserController(); $controller->invoke(); ?>

Our Controller class has only one function for invoking the controller and the constructor. The constructor instantiates a model, it calls the model class to retrieve the data. After that it calls the corresponding passing the data coming from the model to view section. The code is given below.

File Name: UserController.php

<?php include_once("model/User.php"); class UserController{ public $model; public function __construct(){ $this->model = new User(); } public function invoke(){ if(!isset($_GET['user'])){ $users = $this->model->getAllUser(); include 'view/userlist.php'; } else{ $user = $this->model->getUser($_GET['user']); include 'view/viewuser.php'; }}} ?>

More Related Post

MVC programming in PHP

The Model represents the data and the logic of an application. It is responsible for storing, deleting, updating the application data. Generally it includes the operations of database.

In this example the model is represented by one classes: the “User” class. The “User” class is an entity class. Their solely purpose is to keep data. In the above snippet you can notice how Model is returning a specific user, or a list of all the users.

File Name: user.php

<?php class User { public $name; public $city; public $country; public function __construct($name = null,$city = null,$country = null) { $this->name = $name; $this->city = $city; $this->country = $country; } public function getAllUser() { //hardcoded value if you are not using database return array( "Ram" => new User("Ram","Delhi","India"), "John Doe" => new User("John Doe","Boston","USA") ); } public function getUser($name) { $users = $this->getAllUser(); return $users[$name]; } } ?>

MVC in PHP

The view is responsible for formatting the data received from the model in a form accessible to the user.

The controller delegates the data from the model to a specific view element. The view layer can use a template system to render the html pages.

In our example the view contains only two files one for displaying the particular user detail and the other one for displaying a list of all the users.

File Name: userlist.php

<html> <head></head> <title>User List</title> <body> <table> <thead> <tr> <th>Name</th> <th>City</th> <th>Country</th> </tr> </thead> <tbody> <?php foreach ($users as $key => $user) { echo '<tr> <td> <a href="index.php?user='.$user->name.'">'.$user->name.'</a></td> <td>'.$user->city.'</td> <td>'.$user->country.'</td> </tr>'; } ?> </tbody> </table> </body> </html>



File Name: viewuser.php

<html> <head></head> <title>View User</title> <body> <?php echo '<b>Name: </b>' . $user->name . '<br/>'; echo '<b>City: </b>' . $user->city . '<br/>'; echo '<b>Country: </b>' . $user->country . '<br/>'; ?> </body> </html>

More Related Post