21.10.2015 Views

1-33

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Controller<br />

Symfony2 – Franz Jordán 2011<br />

A controller is a PHP function you create that takes information from the HTTP request and<br />

constructs and returns an HTTP response (as a Symfony2 Response object). The response could<br />

be an HTML page, an XML document, a serialized JSON array, an image, a redirect, a 404 error or<br />

anything else you can dream up. The controller contains whatever arbitrary logic your<br />

applicationneeds to render the content of a page.<br />

To see how simple this is, let's look at a Symfony2 controller in action. The following controller<br />

would render a page that simply prints Hello world!:<br />

use Symfony\Component\HttpFoundation\Response;<br />

public function helloAction()<br />

{<br />

return new Response('Hello world!');<br />

}<br />

The goal of a controller is always the same: create and return a Response object. Along the<br />

way, it might read information from the request, load a database resource, send an email, or set<br />

information on the user's session. But in all cases, the controller will eventually return<br />

the Responseobject that will be delivered back to the client.<br />

There's no magic and no other requirements to worry about! Here are a few common examples:<br />

Controller A prepares a Response object representing the content for the homepage of<br />

the site.<br />

Controller B reads the slug parameter from the request to load a blog entry from the<br />

database and create a Response object displaying that blog. If the slug can't be found<br />

in the database, it creates and returns a Response object with a 404 status code.<br />

Controller C handles the form submission of a contact form. It reads the form information<br />

from the request, saves the contact information to the database and emails the contact<br />

information to the webmaster. Finally, it creates a Response object that redirects the<br />

client's browser to the contact form "thank you" page.<br />

Requests, Controller, Response Lifecycle<br />

Every request handled by a Symfony2 project goes through the same simple lifecycle. The<br />

framework takes care of the repetitive tasks and ultimately executes a controller, which houses<br />

your custom application code:<br />

1. Each request is handled by a single front controller file (e.g. app.php or app_dev.php)<br />

that's bootstraps the application;<br />

2. The Router reads information from the request (e.g. the URI), finds a route that matches<br />

that information, and reads the _controller parameter from the route;<br />

3. The controller from the matched route is executed and the code inside the controller<br />

creates and returns a Response object;<br />

4. The HTTP headers and content of the Response object are sent back to the client.<br />

Creating a page is as easy as creating a controller (#3) and making a route that maps a URL to<br />

that controller (#2).<br />

Though similarly named, a "front controller" is different from the "controllers" we'll talk about in<br />

this chapter. A front controller is a short PHP file that lives in your web directory and through<br />

45

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

Saved successfully!

Ooh no, something went wrong!