Skip to content

Instantly share code, notes, and snippets.

@louismrose
Created June 16, 2013 16:18
Show Gist options
  • Save louismrose/5792519 to your computer and use it in GitHub Desktop.
Save louismrose/5792519 to your computer and use it in GitHub Desktop.
<?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