symfony2 路由中带点的模式 [英] Pattern with dots in symfony2 routing

查看:22
本文介绍了symfony2 路由中带点的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个捆绑包:一个用于我的网站,一个用于我的 API.我的 API URL 是 mywebsite,我的网站 URL 是 www.website.com.

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.

我使用 host 参数按主机过滤路由,并且我想在我的包之一上允许 localhost 用于本地开发目的(不创建虚拟主机).

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).

但是得到这个错误:

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 ?

这是我的主路由文件:

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:   /

问候,

推荐答案

默认域值不影响路由是否匹配.由于域如果总是出现在请求中,它仅对生成 URL 有用.为此有一些要求(顺便说一句,[^.]++ 将是域的默认要求).

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:   /

它应该可以工作.但是所有路由都将使用 mywebsite.fr 生成(在绝对 url 的情况下).您可以使用服务容器参数.

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:   /

例如,定义此参数的最简单方法是在 parameter.yml 中.或者我认为您也可以完全省略默认键.

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

这篇关于symfony2 路由中带点的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆