Raphaël Malié
Raphaël Malié

Reputation: 4012

Pattern with dots in symfony2 routing

I have 2 bundles : one for my website, and one for my API. My API URL is mywebsite and my website URL is www.website.com.

I use the host parameter to filter routes by host, and I want to allow localhost on one of my bundle for local development purpose (without creating a vhost).

But get this error :

Parameter "domain" for route "tv_home" must match "[^\.]++" ("((www\.)?mywebsite\.fr|localhost)" given) to generate a corresponding URL.

I understand that it comes from the regex I use here :

domain: "((www\.)?mywebsite\.fr|localhost)"

How can I achieve my goal if I can't use dots in this regex ?

Here is my master routing file :

tv_api:
    resource: "@TVApiBundle/Resources/config/routing.yml"
    host:     "api.mywebsite.fr"
    prefix:   /

tv_site:
    resource: "@TVSiteBundle/Resources/config/routing.yml"
    host:     "{domain}"
    defaults:
            domain: "((www\.)?mywebsite\.fr|localhost)"
    prefix:   /

Regards,

Upvotes: 2

Views: 953

Answers (1)

kba
kba

Reputation: 4310

Default domain value does not affect is route will match or not. Because of domain if always present in request it is useful only for generating URLs. There are requirements for that purpose (btw [^.]++ would be default requirement for domain).

tv_site:
    resource: "@TVSiteBundle/Resources/config/routing.yml"
    host:     "{domain}"
    defaults:
        domain: "mywebsite.fr"
    requirements:
        domain: "((www\.)?mywebsite\.fr|localhost)"
    prefix:   /

It should work. But all routes will be generated with mywebsite.fr (in case of absolute url). You can use service container parameter.

tv_site:
    resource: "@TVSiteBundle/Resources/config/routing.yml"
    host:     "{domain}"
    defaults:
        domain: "%domain%"
    requirements:
        domain: "((www\.)?mywebsite\.fr|localhost)"
    prefix:   /

As example easiest way to define this parameter is in parameter.yml. Or I think you can also ommit defaults key completely.

Upvotes: 3

Related Questions