Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP supports MVP pattern?

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?

like image 525
Ven Avatar asked Dec 25 '10 10:12

Ven


People also ask

What is MVP in PHP?

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern, and is used mostly for building user interfaces.

What is the MVP design pattern?

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.

Is MVP better than MVC?

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.

Is flutter MVC or MVP?

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.


2 Answers

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.

like image 53
Yang Avatar answered Sep 20 '22 15:09

Yang


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.
like image 28
mario Avatar answered Sep 21 '22 15:09

mario