Skip to content

Commit a323579

Browse files
committed
Merge pull request symfony#3125 from WouterJ/hostname_wording_fix
Fix hostname matching articles
2 parents 0447f56 + d037150 commit a323579

File tree

2 files changed

+140
-25
lines changed

2 files changed

+140
-25
lines changed

book/routing.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,8 @@ form via the same URL, while using distinct controllers for the two actions.
707707

708708
If no ``methods`` are specified, the route will match on *all* methods.
709709

710-
Adding a Host
711-
~~~~~~~~~~~~~
710+
Adding a Host Requirement
711+
~~~~~~~~~~~~~~~~~~~~~~~~~
712712

713713
.. versionadded:: 2.2
714714
Host matching support was added in Symfony 2.2
@@ -1067,8 +1067,8 @@ from the new routing resource.
10671067
:doc:`FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/routing>`
10681068
to see how.
10691069

1070-
Adding a Host regex to Imported Routes
1071-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1070+
Adding a Host requirement to Imported Routes
1071+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10721072

10731073
.. versionadded:: 2.2
10741074
Host matching support was added in Symfony 2.2

components/routing/hostname_pattern.rst

Lines changed: 136 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,79 @@ You can also match on the HTTP *host* of the incoming request.
6060
Both routes match the same path ``/``, however the first one will match
6161
only if the host is ``m.example.com``.
6262

63-
Placeholders and Requirements in Hostname Patterns
64-
--------------------------------------------------
63+
Using Placeholders
64+
------------------
6565

66-
If you're using the :doc:`DependencyInjection Component </components/dependency_injection/index>`
67-
(or the full Symfony2 Framework), then you can use
68-
:ref:`service container parameters <book-service-container-parameters>` as
69-
variables anywhere in your routes.
66+
The host option uses the same syntax as the path matching system. This means
67+
you can use placeholders in your hostname:
7068

71-
You can avoid hardcoding the domain name by using a placeholder and a requirement.
72-
The ``%domain%`` in requirements is replaced by the value of the ``domain``
73-
dependency injection container parameter.
69+
.. configuration-block::
70+
71+
.. code-block:: yaml
72+
73+
projects_homepage:
74+
path: /
75+
host: "{project_name}.example.com"
76+
defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
77+
78+
homepage:
79+
path: /
80+
defaults: { _controller: AcmeDemoBundle:Main:homepage }
81+
82+
.. code-block:: xml
83+
84+
<?xml version="1.0" encoding="UTF-8" ?>
85+
86+
<routes xmlns="http://symfony.com/schema/routing"
87+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88+
xsi:schemaLocation="http://symfony.com/schema/routing
89+
http://symfony.com/schema/routing/routing-1.0.xsd"
90+
>
91+
92+
<route id="projects_homepage" path="/" host="{project_name}.example.com">
93+
<default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
94+
</route>
95+
96+
<route id="homepage" path="/">
97+
<default key="_controller">AcmeDemoBundle:Main:homepage</default>
98+
</route>
99+
</routes>
100+
101+
.. code-block:: php
102+
103+
use Symfony\Component\Routing\RouteCollection;
104+
use Symfony\Component\Routing\Route;
105+
106+
$collection = new RouteCollection();
107+
$collection->add('project_homepage', new Route('/', array(
108+
'_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
109+
), array(), array(), '{project_name}.example.com'));
110+
111+
$collection->add('homepage', new Route('/', array(
112+
'_controller' => 'AcmeDemoBundle:Main:homepage',
113+
)));
114+
115+
return $collection;
116+
117+
You can also set requirements and default options for these placeholders. For
118+
instance, if you want to match both ``m.example.com`` and
119+
``mobile.example.com``, you use this:
74120

75121
.. configuration-block::
76122

77123
.. code-block:: yaml
78124
79125
mobile_homepage:
80126
path: /
81-
host: m.{domain}
82-
defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
127+
host: "{subdomain}.example.com"
128+
defaults:
129+
_controller: AcmeDemoBundle:Main:mobileHomepage
130+
subdomain: m
83131
requirements:
84-
domain: %domain%
132+
subdomain: m|mobile
85133
86134
homepage:
87-
path: /
135+
path: /
88136
defaults: { _controller: AcmeDemoBundle:Main:homepage }
89137
90138
.. code-block:: xml
@@ -93,11 +141,15 @@ dependency injection container parameter.
93141
94142
<routes xmlns="http://symfony.com/schema/routing"
95143
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
96-
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
144+
xsi:schemaLocation="http://symfony.com/schema/routing
145+
http://symfony.com/schema/routing/routing-1.0.xsd"
146+
>
97147
98-
<route id="mobile_homepage" path="/" host="m.example.com">
148+
<route id="mobile_homepage" path="/" host="{subdomain}.example.com">
99149
<default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
100-
<requirement key="domain">%domain%</requirement>
150+
<default key="subdomain">m</default>
151+
152+
<requirement key="subdomain">m|mobile</requirement>
101153
</route>
102154
103155
<route id="homepage" path="/">
@@ -113,22 +165,85 @@ dependency injection container parameter.
113165
$collection = new RouteCollection();
114166
$collection->add('mobile_homepage', new Route('/', array(
115167
'_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
168+
'subdomain' => 'm',
116169
), array(
117-
'domain' => '%domain%',
118-
), array(), 'm.{domain}'));
170+
'subdomain' => 'm|mobile',
171+
), array(), '{subdomain}.example.com'));
119172
120173
$collection->add('homepage', new Route('/', array(
121174
'_controller' => 'AcmeDemoBundle:Main:homepage',
122175
)));
123176
124177
return $collection;
125178
179+
.. tip::
180+
181+
Make sure you also include a default option for the ``subdomain``
182+
placeholder, otherwise you need to include the subdomains value each time
183+
you generate the route.
184+
185+
.. sidebar:: Using Service Parameters
186+
187+
You can also use service parameters if you do not want to hardcode the
188+
hostname:
189+
190+
.. configuration-block::
191+
192+
.. code-block:: yaml
193+
194+
mobile_homepage:
195+
path: /
196+
host: "m.{domain}"
197+
defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
198+
requirements:
199+
domain: "%domain%"
200+
201+
homepage:
202+
path: /
203+
defaults: { _controller: AcmeDemoBundle:Main:homepage }
204+
205+
.. code-block:: xml
206+
207+
<?xml version="1.0" encoding="UTF-8" ?>
208+
209+
<routes xmlns="http://symfony.com/schema/routing"
210+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
211+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
212+
213+
<route id="mobile_homepage" path="/" host="m.example.com">
214+
<default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
215+
<requirement key="domain">%domain%</requirement>
216+
</route>
217+
218+
<route id="homepage" path="/">
219+
<default key="_controller">AcmeDemoBundle:Main:homepage</default>
220+
</route>
221+
</routes>
222+
223+
.. code-block:: php
224+
225+
use Symfony\Component\Routing\RouteCollection;
226+
use Symfony\Component\Routing\Route;
227+
228+
$collection = new RouteCollection();
229+
$collection->add('mobile_homepage', new Route('/', array(
230+
'_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
231+
), array(
232+
'domain' => '%domain%',
233+
), array(), 'm.{domain}'));
234+
235+
$collection->add('homepage', new Route('/', array(
236+
'_controller' => 'AcmeDemoBundle:Main:homepage',
237+
)));
238+
239+
return $collection;
240+
126241
.. _component-routing-host-imported:
127242

128-
Adding a Host Regex to Imported Routes
129-
--------------------------------------------
243+
Using Host Matching of Imported Routes
244+
--------------------------------------
130245

131-
You can set a host regex on imported routes:
246+
You can also set the host option on imported routes:
132247

133248
.. configuration-block::
134249

0 commit comments

Comments
 (0)