1-33
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Symfony2 – Franz Jordán 2011<br />
use Symfony\Component\HttpFoundation\Request;<br />
public function updateAction(Request $request)<br />
{<br />
$form = $this->createForm(...);<br />
}<br />
$form->bindRequest($request);<br />
// ...<br />
The Base Controller Class<br />
For convenience, Symfony2 comes with a base Controller class that assists with some of the<br />
most common controller tasks and gives your controller class access to any resource it might<br />
need. By extending this Controller class, you can take advantage of several helper methods.<br />
Add the use statement atop the Controller class and then modify the HelloController to<br />
extend it:<br />
// src/Acme/HelloBundle/Controller/HelloController.php<br />
namespace Acme\HelloBundle\Controller;<br />
use Symfony\Bundle\FrameworkBundle\Controller\Controller;<br />
use Symfony\Component\HttpFoundation\Response;<br />
class HelloController extends Controller<br />
{<br />
public function indexAction($name)<br />
{<br />
return new Response('Hello '.$name.'!');<br />
}<br />
}<br />
This doesn't actually change anything about how your controller works. In the next section,<br />
you'll learn about the helper methods that the base controller class makes available. These<br />
methods are just shortcuts to using core Symfony2 functionality that's available to you with or<br />
without the use of the base Controller class. A great way to see the core functionality in<br />
action is to look in theController class itself.<br />
Extending the base class is optional in Symfony; it contains useful shortcuts but nothing<br />
mandatory. You can also<br />
extendSymfony\Component\DependencyInjection\ContainerAware. The service<br />
container object will then be accessible via the container property.<br />
You can also define your Controllers as Services.<br />
Common Controller Tasks<br />
Though a controller can do virtually anything, most controllers will perform the same basic tasks<br />
over and over again. These tasks, such as redirecting, forwarding, rendering templates and<br />
accessing core services, are very easy to manage in Symfony2.<br />
49