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>
<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>
Nice Post !!!
Thanks a lot, KiaRoyaw
ok naa
Great Post Developer Helps.
Thanks for the great post!!!
Thanks Cesspymn2
Very useful post!!!
Thanks for your feedback!!!
Pretty! This was a really wonderful post. Thank you for providing this information.
Nice Post!!!
This is a topic that is close to my heart… Best wishes!
Exactly where are your contact details though?
Enjoyed every bit of your post. Thanks Again. Fantastic.
Hello there, You have done an incredible job. Iβll certainly Digg it and personally suggest to my friends. Iβm confident they will be benefited from this website.
What a wonderful post you have written, thanks for sharing!
First time visiting your website, I love your web site!
Hey, This article posted at this website is really good.
Im grateful for the article post. Awesome.
Great looking website. Assume you did a great deal of your verry
own coding.
My brother recommended I might like this web site. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!
Appreciate you sharing, great blog article. Really thank you!
Pretty! This was a really wonderful article. Many thanks for providing this info.
Very nice article. I certainly love this site. Continue the good work!
Good blog post. I absolutely love this site. Thanks!
Thanks, I’ve been looking for this for a long time