Skip to content

Instantly share code, notes, and snippets.

@funkytaco
Created August 8, 2015 20:58
Show Gist options
  • Save funkytaco/87fd34b5ef863ebbc120 to your computer and use it in GitHub Desktop.
Save funkytaco/87fd34b5ef863ebbc120 to your computer and use it in GitHub Desktop.
Example PHP-MVC controller
<?php
namespace Main\Controllers;
use \Klein\Request;
use \Klein\Response;
use \Main\Renderer\Renderer;
//use \Main\PDO;
use \Main\Mock\PDO;
/**
* NOTE that the following are injected into your controller
* Renderer $renderer - Template Engine
* PDO $conn - PDO
* Dependency Injecting makes testing easier!
***/
class IndexController implements IController {
private $data;
use \Main\Traits\DemoData;
use \Main\Traits\MenuData;
public function __construct(
Renderer $renderer,
PDO $conn
) {
$this->renderer = $renderer;
$this->conn = $conn;
$this->data = [
'appName' => self::appName() //from DemoData.php
];
}
public function get(Request $request, Response $response) {
$this->data['demo_menu'] = self::getDemoMenu('index');
$html = $this->renderer->render('index', $this->data);
$response->body($html);
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment