How to Match a Route Based on the Host
How to Match a Route Based on the Host¶
You can also match on the HTTP host of the incoming request.
Both routes match the same path
/, however the first one will match
only if the host is
The host option uses the same syntax as the path matching system. This means you can use placeholders in your hostname:
You can also set requirements and default options for these placeholders. For
instance, if you want to match both
mobile.example.com, you use this:
You can also use service parameters if you do not want to hardcode the hostname:
Make sure you also include a default option for the
otherwise you need to include a domain value each time you generate
a URL using the route.
Using Host Matching of Imported Routes¶
You can also set the host option on imported routes:
hello.example.com will be set on each route loaded from the new
Testing your Controllers¶
You need to set the Host HTTP header on your request objects if you want to get past url matching in your functional tests.
1 2 3 4 5 6 7
$crawler = $client->request( 'GET', '/homepage', array(), array(), array('HTTP_HOST' => 'm.' . $client->getContainer()->getParameter('domain')) );
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License .