symfony2 Routing Subdomain-based routing


Example

Subdomain-based routing can be handled in Symfony using host parameter. For example, _locale parameter can be used as subdomain value.

Assuming

locale: en
domain: somedomain.com

parameters are defined in parameters.yml config file, route would be:

/**
 * @Route(
 *      "/",
 *      name="homepage",
 *      host="{_locale}.{domain}",
 *      defaults={"_locale" = "%locale%", "domain" = "%domain%"},
 *      requirements={"_locale" = "%locale%|de|fr", "domain" = "%domain%"}
 * )
 * @Route(
 *      "/",
 *      name="homepage_default",
 *      defaults={"_locale" = "%locale%"}
 * )
 */

From this point router can handle URI's such as http://de.somedomain.com. Second @Route annotation can be used as a fallback for default locale and void subdomain, http://somedomain.com.