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

Congratulations! You've just created your first route and connected it to a controller. Now, when<br />

you visit /blog/my-post, the showAction controller will be executed and the $slug variable<br />

will be equal to my-post.<br />

This is the goal of the Symfony2 router: to map the URL of a request to a controller. Along the<br />

way, you'll learn all sorts of tricks that make mapping even the most complex URLs easy.<br />

Routing: Under the Hood<br />

When a request is made to your application, it contains an address to the exact "resource" that<br />

the client is requesting. This address is called the URL, (or URI), and could<br />

be /contact, /blog/read-me, or anything else. Take the following HTTP request for<br />

example:<br />

GET /blog/my-blog-post<br />

The goal of the Symfony2 routing system is to parse this URL and determine which controller<br />

should be executed. The whole process looks like this:<br />

1. The request is handled by the Symfony2 front controller (e.g. app.php);<br />

2. The Symfony2 core (i.e. Kernel) asks the router to inspect the request;<br />

3. The router matches the incoming URL to a specific route and returns information about the<br />

route, including the controller that should be executed;<br />

4. The Symfony2 Kernel executes the controller, which ultimately returns a Response object.<br />

56

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

Saved successfully!

Ooh no, something went wrong!