Skip to content

Commit 186c335

Browse files
committed
moved the HTTP scheme paragraph to its own cookbook chapter
1 parent 5d2563c commit 186c335

File tree

2 files changed

+76
-57
lines changed

2 files changed

+76
-57
lines changed

book/routing.rst

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -682,63 +682,6 @@ 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-
742685
.. index::
743686
single: Routing; Advanced example
744687
single: Routing; _format parameter

cookbook/routing/scheme.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. index::
2+
single: Routing; Scheme requirement
3+
4+
How to force routes to always use HTTPS
5+
=======================================
6+
7+
Sometimes, you want to secure some routes and be sure that they are always
8+
accessed via the HTTPS protocol. The Routing allows you to enforce the HTTP
9+
scheme via the ``_scheme`` requirement:
10+
11+
.. configuration-block::
12+
13+
.. code-block:: yaml
14+
15+
secure:
16+
pattern: /secure
17+
defaults: { _controller: AcmeDemoBundle:Main:secure }
18+
requirements:
19+
_scheme: https
20+
21+
.. code-block:: xml
22+
23+
<?xml version="1.0" encoding="UTF-8" ?>
24+
25+
<routes xmlns="http://symfony.com/schema/routing"
26+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
28+
29+
<route id="secure" pattern="/secure">
30+
<default key="_controller">AcmeDemoBundle:Main:secure</default>
31+
<requirement key="_scheme">https</requirement>
32+
</route>
33+
</routes>
34+
35+
.. code-block:: php
36+
37+
use Symfony\Component\Routing\RouteCollection;
38+
use Symfony\Component\Routing\Route;
39+
40+
$collection = new RouteCollection();
41+
$collection->add('secure', new Route('/secure', array(
42+
'_controller' => 'AcmeDemoBundle:Main:secure',
43+
), array(
44+
'_scheme' => 'https',
45+
)));
46+
47+
return $collection;
48+
49+
The above configuration forces the ``secure`` route to always use HTTPS.
50+
51+
When generating the ``secure`` URL, and if the current scheme is HTTP, Symfony
52+
will automatically generate an absolute URL with HTTPS as the scheme:
53+
54+
.. code-block:: text
55+
56+
# If the current scheme is HTTPS
57+
{{ path('secure') }}
58+
# generates /secure
59+
60+
# If the current scheme is HTTP
61+
{{ path('secure') }}
62+
# generates https://example.com/secure
63+
64+
The requirement is also enforced for incoming requests. If you try to access
65+
the ``/secure`` path with HTTP, you will automatically be redirected to the
66+
same URL but with the HTTPS scheme.
67+
68+
The above example uses ``https`` for the ``_scheme``, but you can also force a
69+
URL to always use ``http``.
70+
71+
.. note::
72+
73+
The Security component provides another way to enforce the HTTP scheme via
74+
the ``requires_channel`` setting. This alternative method is better suited
75+
to secure an "area" of your website (all URLs under ``/admin``) or when
76+
you want to secure URLs defined in a third party bundle.

0 commit comments

Comments
 (0)