There are lot of examples explaining MVP pattern using ASP.NET but not found anything using PHP.
I am PHP programmer and want to know where can I get MVP pattern PHP examples?
Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern, and is used mostly for building user interfaces.
Model View Presenter (MVP) The MVP pattern is similar to the MVC pattern. It is derived from MVC pattern, wherein the controller is replaced by the presenter. This pattern divides an application into three major aspects: Model, View, and Presenter.
MVP pattern overcomes the challenges of MVC and provides an easy way to structure the project codes. The reason why MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase. It is composed of the following three components: Model: Layer for storing data.
The MVP architecture pattern is a derivation from the MVC pattern wherein the Controller is replaced by the Presenter. The MVP divides an application into three layers: Model, View, and Presenter.
The short answer is: Yes PHP does.
(Note, its not exactly MVP as described in its original paper, but a variation for web)
The difference between MVC and MVP is that, a view is totally passive and unaware of the model layer. While in MVC it isn't passive and aware of the Model Layer. In proper MVP, View
class (if it is) also SHOULD NOT implement a constructor.
Note: A model in real-world scenario is not class, but abstraction layer, that contain a lot of classes to deal with application logic. I'd call it "Model" for demonstration purposes.
class Model
{
public function getSomeStuff()
{
return array('foo' => 'bar');
}
}
class View
{
public function render($path, array $vars = array())
{
ob_start();
extract($vars);
require($path);
return ob_get_clean();
}
}
class Presenter
{
private $model;
private $view;
public function __construct(Model $model, View $view)
{
$this->model = $model;
$this->view = $view;
}
public function indexAction()
{
$data = $this->model->getSomeStuff();
// Variables are set now, render the HTML
// And returns as a string
return $this->view->render('path/to/template.phtml', $data);
}
}
File: template.phtml
<!DOCTYPE html>
<html>
<head>
<title>...</title>
</head>
<body>
<?php foreach($vars as $key => $value): ?>
<p><?php echo $key; ?> : <?php echo $value; ?></p>
<?php endforeach; ?>
</body>
</html>
And the usage is:
$model = new Model();
$view = new View();
$presenter = new Presenter($service, $view);
echo $presenter->indexAction();
Note that, this is very simplified example. In real-world scenario, any MVP-based application SHOULD also implement things like: Router, SPL class autoloader.
MVP and MVC both are actually meant for GUI apps. Most PHP frameworks use "MVC" more as buzzword. The actual implementation with dumb models (just database), non-active views (= templates) and orchestrating controllers actually matches MVP already. And functionality-wise controllers often function as presenters anyway, shoveling data from models into views. (In proper MVC the model and view interact more, with the view actually being the active component).
But anyway, there are a few frameworks which are actually aware of the newer terminology and pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With