Does PHP support the MVP pattern?

There are many examples explaining the MVP pattern using ASP.NET but nothing found using PHP.

I am a PHP programmer and want to know where can I get examples of PHP MVP templates?

+4
php design-patterns mvp
source share
4 answers

MVP and MVC both actually mean for GUI applications. Most PHP frameworks use "MVC" more as a buzzword . The actual implementation with dumb models (just a database), inactive views (= templates) and orchestral controllers actually correspond to MVP. And functional controllers often work as lead ones anyway, dragging data from models to views. (In the correct MVC, the model and view interact more, and the view is actually the active component).

But in any case, there are several frameworks that really know about the new terminology and template.

+10
source share

Short answer: Yes, PHP does .

(Note that this is not exactly MVP, as described in its original article, but an option for a web page)

The difference between MVC and MVP is that the view is completely passive and does not know about the model layer. In MVC, it is not passive and does not know about the model layer. In the correct MVP , View class (if any) also SHOULD NOT implement the constructor.

A typical MVP example will consist of the following parts:

  • Data Access Level (DataMappers, ORM, etc.)
  • Business logic (e.g. validation and computation)
  • Passive presentation class (it may be a template, but it would be better to stick to the class)
  • Host who connects both Model and View

Example how to implement Model-View-Presenter with PHP

<sub> Note. A model in a real scenario is not a class, but an abstraction layer that contains many classes for working with application logic. I would call it "Model" for demonstration purposes. Sub>

 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 following is used:

 $model = new Model(); $view = new View(); $presenter = new Presenter($service, $view); echo $presenter->indexAction(); 

Please note that this is a very simplified example. In a real scenario, any MVP-based application SHOULD also implement such things as: Router, SPL class autoloader.

+10
source share

Check this Mutant Vole PHP library

0
source share

In my opinion, the Lion Framework (www.lionframework.org) is the most mature implementation of MVP at the moment.

0
source share

All Articles