Skip to content

Commit 068de1f

Browse files
committed
added doc for the routing _scheme requirement
1 parent 9268d30 commit 068de1f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

book/routing.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,63 @@ form via the same URL, while using distinct controllers for the two actions.
682682
Like the other requirements, the ``_method`` requirement is parsed as a regular
683683
expression. To match ``GET`` *or* ``POST`` requests, you can use ``GET|POST``.
684684

685+
.. index::
686+
single: Routing; Scheme requirement
687+
688+
Adding HTTP Scheme Requirements
689+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
690+
691+
You can also match on the HTTP *scheme* of the incoming request (i.e. HTTP or
692+
HTTPS). This can be used to force certain routes to be matched and generated
693+
with the given scheme. This can be accomplished with the following route
694+
configuration:
695+
696+
.. configuration-block::
697+
698+
.. code-block:: yaml
699+
700+
secure:
701+
pattern: /secure
702+
defaults: { _controller: AcmeDemoBundle:Main:secure }
703+
requirements:
704+
_scheme: https
705+
706+
.. code-block:: xml
707+
708+
<?xml version="1.0" encoding="UTF-8" ?>
709+
710+
<routes xmlns="http://symfony.com/schema/routing"
711+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
712+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
713+
714+
<route id="secure" pattern="/secure">
715+
<default key="_controller">AcmeDemoBundle:Main:secure</default>
716+
<requirement key="_scheme">https</requirement>
717+
</route>
718+
</routes>
719+
720+
.. code-block:: php
721+
722+
use Symfony\Component\Routing\RouteCollection;
723+
use Symfony\Component\Routing\Route;
724+
725+
$collection = new RouteCollection();
726+
$collection->add('secure', new Route('/secure', array(
727+
'_controller' => 'AcmeDemoBundle:Main:secure',
728+
), array(
729+
'_scheme' => 'https',
730+
)));
731+
732+
return $collection;
733+
734+
The above configuration forces the ``secure`` route to use HTTPS. When
735+
generating the ``secure`` URL, and if the current scheme is HTTP, Symfony will
736+
automatically generate an absolute URL with HTTPS as the scheme.
737+
738+
The requirement is also enforced for incoming requests. If you try to access
739+
the ``/secure`` path with HTTP, you will be automatically redirected to the
740+
same URL but with the HTTPS scheme.
741+
685742
.. index::
686743
single: Routing; Advanced example
687744
single: Routing; _format parameter

0 commit comments

Comments
 (0)