Skip to content

Instantly share code, notes, and snippets.

@stephenmelrose
Last active December 14, 2015 17:09
Show Gist options
  • Save stephenmelrose/5120759 to your computer and use it in GitHub Desktop.
Save stephenmelrose/5120759 to your computer and use it in GitHub Desktop.
MVP design/approach.
<?php
$repository = new Repository();
$data = new Data($repository);
$controller = new Controller($data);
$controller->executeAction($_GET['something']);
<?php
class Repository
{
public function getEntities() {
// Do database query and return results
}
}
class Data
{
protected $repository;
public function __construct(Repository $repository) {
$this->repository = $repository;
}
public function getData($requestParam) {
$entities = $this->repository->getEntities();
// Do some logic on said entities
return array(
'entities' => $entities,
'otherData' => $otherData
);
}
}
class Presenter
{
public function setData(array $data) {
// Store data internally
}
public function render() {
// Convert set data into view data and render HTML using it
return $html;
}
}
class Controller
{
protected $data;
public function __construct(Data $data) {
$this->data = $data;
}
public function executeAction($requestParam) {
$rawData = $data->getData($requestParam);
$presenter = new Presenter();
$presenter->setData($rawData);
return $presenter->render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment