Skip to content

Instantly share code, notes, and snippets.

@Seldaek
Created December 2, 2010 16:26
Show Gist options
  • Save Seldaek/725605 to your computer and use it in GitHub Desktop.
Save Seldaek/725605 to your computer and use it in GitHub Desktop.
class FooController
{
public function viewArticleAction($articleId)
{
$article = $this->articleRepository->getById($articleId);
$parameters = array(
'article' => $article,
);
// beginner
return $this->handle($parameters, 'FooBundle:Foo:view.twig');
// advanced
$view = $this->view;
$view->setParameters($parameters);
$view->setTemplate('FooBundle:Foo:view.twig');
return $view->handle($this->request);
}
}
Example custom handler, note this could also be defined by extending the DefaultView class:
class FooController
{
public function rssFeedAction()
{
// Could be done via DIC config
$this->view->registerHandler('rss', array($this, 'handleRss'));
$data = array('news' => $this->newsRepository->getLatestNews());
// Could be done in the route
$this->request->setRequestFormat('rss');
$this->view->setParameters($data);
return $this->view->handle($this->request);
}
public function handleRss($view, $request, $response)
{
$data = $view->getParameters();
foreach ($data['news'] as $news) {
// build feed content
}
$response->headers->set('Content-Type', 'text/xml');
$response->setContent($feed);
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment