Created
December 2, 2010 16:26
-
-
Save Seldaek/725605 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
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