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

Route Parameters and Controller Arguments<br />

The route parameters (e.g. {slug}) are especially important because each is made available as<br />

an argument to the controller method:<br />

public function showAction($slug)<br />

{<br />

// ...<br />

}<br />

In reality, the entire defaults collection is merged with the parameter values to form a single<br />

array. Each key of that array is available as an argument on the controller.<br />

In other words, for each argument of your controller method, Symfony looks for a route<br />

parameter of that name and assigns its value to that argument. In the advanced example above,<br />

any combination (in any order) of the following variables could be used as arguments to<br />

theshowAction() method:<br />

$culture<br />

$year<br />

$title<br />

$_format<br />

$_controller<br />

Since the placeholders and defaults collection are merged together, even<br />

the $_controllervariable is available. For a more detailed discussion, see Route Parameters<br />

as Controller Arguments.<br />

You can also use a special $_route variable, which is set to the name of the route that was<br />

matched.<br />

Including External Routing Resources<br />

All routes are loaded via a single configuration file -<br />

usually app/config/routing.yml (see Creating Routes above). Commonly, however, you'll<br />

want to load routes from other places, like a routing file that lives inside a bundle. This can be<br />

done by "importing" that file:<br />

# app/config/routing.yml<br />

acme_hello:<br />

resource: "@AcmeHelloBundle/Resources/config/routing.yml"<br />

When importing resources from YAML, the key (e.g. acme_hello) is meaningless. Just be sure<br />

that it's unique so no other lines override it.<br />

The resource key loads the given routing resource. In this example the resource is the full<br />

path to a file, where the @AcmeHelloBundle shortcut syntax resolves to the path of that<br />

bundle. The imported file might look like this:<br />

# src/Acme/HelloBundle/Resources/config/routing.yml<br />

acme_hello:<br />

pattern: /hello/{name}<br />

defaults: { _controller: AcmeHelloBundle:Hello:index }<br />

63

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

Saved successfully!

Ooh no, something went wrong!