Does PHP supports MVP pattern?

一个人想着一个人 提交于 2019-11-30 07:32:38

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.

A typical example of MVP will consist of these parts:

  1. Data Access Layer (DataMappers, ORM etc)
  2. Business Logic (like validation, and computations)
  3. Passive view class (it could be a template, but it would be better to stick with a class)
  4. The presenter that bridges both Model and View

An example, of how to implement Model-View-Presenter with PHP

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.

mario

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.

Check this library Mutant Vole PHP

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!