简体   繁体   English

symfony2路由中带有圆点的图案

[英]Pattern with dots in symfony2 routing

I have 2 bundles : one for my website, and one for my API. 我有2个捆绑包:一个用于我的网站,一个用于我的API。 My API URL is mywebsite and my website URL is www.website.com . 我的API URL是mywebsite ,我的网站URL是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). 我使用host参数按主机过滤路由,并且我想允许我的一个捆绑软件中的localhost用于本地开发目的(不创建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. 由于域(如果始终存在于请求中)仅对生成URL有用。 There are requirements for that purpose (btw [^.]++ would be default requirement for domain). 为此有一些要求(btw [^。] ++将是域的默认要求)。

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). 但是所有路由都将使用mywebsite.fr生成(如果使用绝对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. 例如,定义此参数的最简单方法是在parameter.yml中。 Or I think you can also ommit defaults key completely. 或者我认为您也可以完全省略默认密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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