Created
June 16, 2013 16:18
-
-
Save louismrose/5792519 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class Widget_Controller extends Dev_Controller { | |
public function show() { | |
$params = $this->app->request()->params(); | |
// Here we use a "null object" that initialises all attributes to empty strings or other sensible defaults | |
$this->render(new Widget()); | |
} | |
public function create() { | |
$params = $this->app->request()->params(); | |
$widget = new Widget($params); | |
if (widget->save()) { | |
$this->app->redirect('/somewhere_else'); | |
} else { | |
// Here we pass the invalid, unsaved object back to the template so that it can be re-rendered along with its errors | |
$this->render($widget); | |
} | |
} | |
private function render($widget) { | |
$this->app->render('widget.twig', array( | |
'data' => $widget, | |
'errors' => $widget->getErrors() | |
)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment