简体   繁体   中英

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,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM