@@ -60,31 +60,79 @@ You can also match on the HTTP *host* of the incoming request.
60
60
Both routes match the same path ``/ ``, however the first one will match
61
61
only if the host is ``m.example.com ``.
62
62
63
- Placeholders and Requirements in Hostname Patterns
64
- --------------------------------------------------
63
+ Using Placeholders
64
+ ------------------
65
65
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:
70
68
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:
74
120
75
121
.. configuration-block ::
76
122
77
123
.. code-block :: yaml
78
124
79
125
mobile_homepage :
80
126
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
83
131
requirements :
84
- domain : %domain%
132
+ subdomain : m|mobile
85
133
86
134
homepage :
87
- path : /
135
+ path : /
88
136
defaults : { _controller: AcmeDemoBundle:Main:homepage }
89
137
90
138
.. code-block :: xml
@@ -93,11 +141,15 @@ dependency injection container parameter.
93
141
94
142
<routes xmlns =" http://symfony.com/schema/routing"
95
143
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
+ >
97
147
98
- <route id =" mobile_homepage" path =" /" host =" m .example.com" >
148
+ <route id =" mobile_homepage" path =" /" host =" {subdomain} .example.com" >
99
149
<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 >
101
153
</route >
102
154
103
155
<route id =" homepage" path =" /" >
@@ -113,22 +165,85 @@ dependency injection container parameter.
113
165
$collection = new RouteCollection();
114
166
$collection->add('mobile_homepage', new Route('/', array(
115
167
'_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
168
+ 'subdomain' => 'm',
116
169
), array(
117
- 'domain ' => '%domain% ',
118
- ), array(), 'm.{domain} '));
170
+ 'subdomain ' => 'm|mobile ',
171
+ ), array(), '{subdomain}.example.com '));
119
172
120
173
$collection->add('homepage', new Route('/', array(
121
174
'_controller' => 'AcmeDemoBundle:Main:homepage',
122
175
)));
123
176
124
177
return $collection;
125
178
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
+
126
241
.. _component-routing-host-imported :
127
242
128
- Adding a Host Regex to Imported Routes
129
- --------------------------------------------
243
+ Using Host Matching of Imported Routes
244
+ --------------------------------------
130
245
131
- You can set a host regex on imported routes:
246
+ You can also set the host option on imported routes:
132
247
133
248
.. configuration-block ::
134
249
0 commit comments