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.

Symfony2 – Franz Jordán 2011<br />

Summary<br />

Routing is a system for mapping the URL of incoming requests to the controller function that<br />

should be called to process the request. It both allows you to specify beautiful URLs and keeps<br />

the functionality of your application decoupled from those URLs. Routing is a two-way<br />

mechanism, meaning that it should also be used to generate URLs.<br />

Learn more from the Cookbook<br />

How to force routes to always use<br />

HTTPS<br />

Sometimes, you want to secure some routes and be sure that they are always accessed via the<br />

HTTPS protocol. The Routing component allows you to enforce the HTTP scheme via<br />

the _schemerequirement:<br />

secure:<br />

pattern: /secure<br />

defaults: { _controller: AcmeDemoBundle:Main:secure }<br />

requirements:<br />

_scheme: https<br />

The above configuration forces the secure route to always use HTTPS.<br />

When generating the secure URL, and if the current scheme is HTTP, Symfony will automatically<br />

generate an absolute URL with HTTPS as the scheme:<br />

# If the current scheme is HTTPS<br />

{{ path('secure') }}<br />

# generates /secure<br />

# If the current scheme is HTTP<br />

{{ path('secure') }}<br />

# generates https://example.com/secure<br />

The requirement is also enforced for incoming requests. If you try to access the /secure path<br />

with HTTP, you will automatically be redirected to the same URL, but with the HTTPS scheme.<br />

The above example uses https for the _scheme, but you can also force a URL to always<br />

use http.<br />

The Security component provides another way to enforce the HTTP scheme via<br />

therequires_channel setting. This alternative method is better suited to secure an "area" of<br />

your website (all URLs under /admin) or when you want to secure URLs defined in a third party<br />

bundle.<br />

66

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

Saved successfully!

Ooh no, something went wrong!