Created
August 8, 2015 20:58
-
-
Save funkytaco/87fd34b5ef863ebbc120 to your computer and use it in GitHub Desktop.
Example PHP-MVC controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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