21.10.2015 Views

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 />

The headers property is a HeaderBag object with several useful methods for reading and<br />

mutating the Response headers. The header names are normalized so that using Content-<br />

Type is equivalent to content-type or even content_type.<br />

The Request Object<br />

Besides the values of the routing placeholders, the controller also has access to<br />

the Request object when extending the base Controller class:<br />

$request = $this->getRequest();<br />

$request->isXmlHttpRequest(); // is it an Ajax request?<br />

$request->getPreferredLanguage(array('en', 'fr'));<br />

$request->query->get('page'); // get a $_GET parameter<br />

$request->request->get('page'); // get a $_POST parameter<br />

Like the Response object, the request headers are stored in a HeaderBag object and are easily<br />

accessible.<br />

Final Thoughts<br />

Whenever you create a page, you'll ultimately need to write some code that contains the logic for<br />

that page. In Symfony, this is called a controller, and it's a PHP function that can do anything it<br />

needs in order to return the final Response object that will be returned to the user.<br />

To make life easier, you can choose to extend a base Controller class, which contains<br />

shortcut methods for many common controller tasks. For example, since you don't want to put<br />

HTML code in your controller, you can use the render() method to render and return the<br />

content from a template.<br />

In other chapters, you'll see how the controller can be used to persist and fetch objects from a<br />

database, process form submissions, handle caching and more.<br />

54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!