/components/routing/hostname_pattern.rst

https://github.com/althaus/symfony-docs · ReStructuredText · 285 lines · 205 code · 80 blank · 0 comment · 0 complexity · d230db30d15d40213ef3b22906e52034 MD5 · raw file

  1. .. index::
  2. single: Routing; Matching on Hostname
  3. How to Match a Route Based on the Host
  4. ======================================
  5. You can also match on the HTTP *host* of the incoming request.
  6. .. configuration-block::
  7. .. code-block:: yaml
  8. mobile_homepage:
  9. path: /
  10. host: m.example.com
  11. defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
  12. homepage:
  13. path: /
  14. defaults: { _controller: AcmeDemoBundle:Main:homepage }
  15. .. code-block:: xml
  16. <?xml version="1.0" encoding="UTF-8" ?>
  17. <routes xmlns="http://symfony.com/schema/routing"
  18. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19. xsi:schemaLocation="http://symfony.com/schema/routing
  20. http://symfony.com/schema/routing/routing-1.0.xsd">
  21. <route id="mobile_homepage" path="/" host="m.example.com">
  22. <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
  23. </route>
  24. <route id="homepage" path="/">
  25. <default key="_controller">AcmeDemoBundle:Main:homepage</default>
  26. </route>
  27. </routes>
  28. .. code-block:: php
  29. use Symfony\Component\Routing\RouteCollection;
  30. use Symfony\Component\Routing\Route;
  31. $collection = new RouteCollection();
  32. $collection->add('mobile_homepage', new Route('/', array(
  33. '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
  34. ), array(), array(), 'm.example.com'));
  35. $collection->add('homepage', new Route('/', array(
  36. '_controller' => 'AcmeDemoBundle:Main:homepage',
  37. )));
  38. return $collection;
  39. Both routes match the same path ``/``, however the first one will match
  40. only if the host is ``m.example.com``.
  41. Using Placeholders
  42. ------------------
  43. The host option uses the same syntax as the path matching system. This means
  44. you can use placeholders in your hostname:
  45. .. configuration-block::
  46. .. code-block:: yaml
  47. projects_homepage:
  48. path: /
  49. host: "{project_name}.example.com"
  50. defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
  51. homepage:
  52. path: /
  53. defaults: { _controller: AcmeDemoBundle:Main:homepage }
  54. .. code-block:: xml
  55. <?xml version="1.0" encoding="UTF-8" ?>
  56. <routes xmlns="http://symfony.com/schema/routing"
  57. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58. xsi:schemaLocation="http://symfony.com/schema/routing
  59. http://symfony.com/schema/routing/routing-1.0.xsd">
  60. <route id="projects_homepage" path="/" host="{project_name}.example.com">
  61. <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
  62. </route>
  63. <route id="homepage" path="/">
  64. <default key="_controller">AcmeDemoBundle:Main:homepage</default>
  65. </route>
  66. </routes>
  67. .. code-block:: php
  68. use Symfony\Component\Routing\RouteCollection;
  69. use Symfony\Component\Routing\Route;
  70. $collection = new RouteCollection();
  71. $collection->add('project_homepage', new Route('/', array(
  72. '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
  73. ), array(), array(), '{project_name}.example.com'));
  74. $collection->add('homepage', new Route('/', array(
  75. '_controller' => 'AcmeDemoBundle:Main:homepage',
  76. )));
  77. return $collection;
  78. You can also set requirements and default options for these placeholders. For
  79. instance, if you want to match both ``m.example.com`` and
  80. ``mobile.example.com``, you use this:
  81. .. configuration-block::
  82. .. code-block:: yaml
  83. mobile_homepage:
  84. path: /
  85. host: "{subdomain}.example.com"
  86. defaults:
  87. _controller: AcmeDemoBundle:Main:mobileHomepage
  88. subdomain: m
  89. requirements:
  90. subdomain: m|mobile
  91. homepage:
  92. path: /
  93. defaults: { _controller: AcmeDemoBundle:Main:homepage }
  94. .. code-block:: xml
  95. <?xml version="1.0" encoding="UTF-8" ?>
  96. <routes xmlns="http://symfony.com/schema/routing"
  97. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  98. xsi:schemaLocation="http://symfony.com/schema/routing
  99. http://symfony.com/schema/routing/routing-1.0.xsd">
  100. <route id="mobile_homepage" path="/" host="{subdomain}.example.com">
  101. <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
  102. <default key="subdomain">m</default>
  103. <requirement key="subdomain">m|mobile</requirement>
  104. </route>
  105. <route id="homepage" path="/">
  106. <default key="_controller">AcmeDemoBundle:Main:homepage</default>
  107. </route>
  108. </routes>
  109. .. code-block:: php
  110. use Symfony\Component\Routing\RouteCollection;
  111. use Symfony\Component\Routing\Route;
  112. $collection = new RouteCollection();
  113. $collection->add('mobile_homepage', new Route('/', array(
  114. '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
  115. 'subdomain' => 'm',
  116. ), array(
  117. 'subdomain' => 'm|mobile',
  118. ), array(), '{subdomain}.example.com'));
  119. $collection->add('homepage', new Route('/', array(
  120. '_controller' => 'AcmeDemoBundle:Main:homepage',
  121. )));
  122. return $collection;
  123. .. tip::
  124. You can also use service parameters if you do not want to hardcode the
  125. hostname:
  126. .. configuration-block::
  127. .. code-block:: yaml
  128. mobile_homepage:
  129. path: /
  130. host: "m.{domain}"
  131. defaults:
  132. _controller: AcmeDemoBundle:Main:mobileHomepage
  133. domain: "%domain%"
  134. requirements:
  135. domain: "%domain%"
  136. homepage:
  137. path: /
  138. defaults: { _controller: AcmeDemoBundle:Main:homepage }
  139. .. code-block:: xml
  140. <?xml version="1.0" encoding="UTF-8" ?>
  141. <routes xmlns="http://symfony.com/schema/routing"
  142. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  143. xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
  144. <route id="mobile_homepage" path="/" host="m.{domain}">
  145. <default key="_controller">AcmeDemoBundle:Main:mobileHomepage</default>
  146. <default key="domain">%domain%</default>
  147. <requirement key="domain">%domain%</requirement>
  148. </route>
  149. <route id="homepage" path="/">
  150. <default key="_controller">AcmeDemoBundle:Main:homepage</default>
  151. </route>
  152. </routes>
  153. .. code-block:: php
  154. use Symfony\Component\Routing\RouteCollection;
  155. use Symfony\Component\Routing\Route;
  156. $collection = new RouteCollection();
  157. $collection->add('mobile_homepage', new Route('/', array(
  158. '_controller' => 'AcmeDemoBundle:Main:mobileHomepage',
  159. 'domain' => '%domain%',
  160. ), array(
  161. 'domain' => '%domain%',
  162. ), array(), 'm.{domain}'));
  163. $collection->add('homepage', new Route('/', array(
  164. '_controller' => 'AcmeDemoBundle:Main:homepage',
  165. )));
  166. return $collection;
  167. .. tip::
  168. Make sure you also include a default option for the ``domain`` placeholder,
  169. otherwise you need to include a domain value each time you generate
  170. a URL using the route.
  171. .. _component-routing-host-imported:
  172. Using Host Matching of Imported Routes
  173. --------------------------------------
  174. You can also set the host option on imported routes:
  175. .. configuration-block::
  176. .. code-block:: yaml
  177. acme_hello:
  178. resource: "@AcmeHelloBundle/Resources/config/routing.yml"
  179. host: "hello.example.com"
  180. .. code-block:: xml
  181. <?xml version="1.0" encoding="UTF-8" ?>
  182. <routes xmlns="http://symfony.com/schema/routing"
  183. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  184. xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
  185. <import resource="@AcmeHelloBundle/Resources/config/routing.xml" host="hello.example.com" />
  186. </routes>
  187. .. code-block:: php
  188. use Symfony\Component\Routing\RouteCollection;
  189. $collection = new RouteCollection();
  190. $collection->addCollection($loader->import("@AcmeHelloBundle/Resources/config/routing.php"), '', array(), array(), array(), 'hello.example.com');
  191. return $collection;
  192. The host ``hello.example.com`` will be set on each route loaded from the new
  193. routing resource.
  194. Testing your Controllers
  195. ------------------------
  196. You need to set the Host HTTP header on your request objects if you want to get
  197. past url matching in your functional tests.
  198. .. code-block:: php
  199. $crawler = $client->request(
  200. 'GET',
  201. '/homepage',
  202. array(),
  203. array(),
  204. array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain'))
  205. );