1-33
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Symfony2 – Franz Jordán 2011<br />
}<br />
And just like when creating a controller for a route, the order of the arguments<br />
to fancyActiondoesn't matter. Symfony2 matches the index key names (e.g. name) with the<br />
method argument names (e.g. $name). If you change the order of the arguments, Symfony2 will<br />
still pass the correct value to each variable.<br />
Like other base Controller methods, the forward method is just a shortcut for core<br />
Symfony2 functionality. A forward can be accomplished directly via the http_kernelservice. A<br />
forward returns a Response object:<br />
$httpKernel = $this->container->get('http_kernel');<br />
$response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(<br />
'name' => $name,<br />
'color' => 'green',<br />
));<br />
Rendering Templates<br />
Though not a requirement, most controllers will ultimately render a template that's responsible<br />
for generating the HTML (or other format) for the controller. The renderView() method<br />
renders a template and returns its content. The content from the template can be used to create<br />
a Responseobject:<br />
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' =><br />
$name));<br />
return new Response($content);<br />
This can even be done in just one step with the render() method, which returns<br />
a Response object containing the content from the template:<br />
return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));<br />
In both cases, the Resources/views/Hello/index.html.twig template inside<br />
the AcmeHelloBundlewill be rendered.<br />
The Symfony templating engine is explained in great detail in the Templating chapter.<br />
The renderView method is a shortcut to direct use of the templating service.<br />
Thetemplating service can also be used directly:<br />
$templating = $this->get('templating');<br />
$content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' =><br />
$name));<br />
Accessing other Services<br />
When extending the base controller class, you can access any Symfony2 service via<br />
the get()method. Here are several common services you might need:<br />
$request = $this->getRequest();<br />
$response = $this->get('response');<br />
$templating = $this->get('templating');<br />
$router = $this->get('router');<br />
51