开发者

Does PHP supports MVP pattern?

开发者 https://www.devze.com 2023-01-31 23:11 出处:网络
There are lot of examples explaining MVP pattern using ASP.NET but 开发者_运维百科not found anything using PHP.

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?


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.


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.

  • http://www.lionframework.org/ said so, but I haven't actually looked at that
  • Nette framework I think
  • http://code.google.com/p/mvp-php/
  • and Opendelight resembles Model-Pipe-ViewController
  • Or have a look at http://matrix.include-once.org/framework/ - those that aren't listed with UnshapedMVC or PassiveMVC are worth checking out.


Check this library Mutant Vole PHP


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

0

精彩评论

暂无评论...
验证码 换一张
取 消