从Symfony 2中的子域生成URL的错误?

时间:2014-03-08 14:57:21

标签: symfony routing symfony-2.3 symfony-routing

我的项目有一个子域名shop.example.comrouting.yml正在使用主机匹配功能(需要Symfony> = 2.2),如here所述:

example_shop:
    resource: "@MyShopBundle/Controller/"
    type:     annotation
    prefix:   /
    host:     "shop.example.com"

example_front:
    resource: "@MyFrontBundle/Controller/"
    type:     annotation
    prefix:   /

网址匹配工作正常,但我有一些无法生成完整/相对网址(将域名链接到子域名,反之亦然):

<pre>shop_index:  {{ url('shop_index') }}</pre>
<pre>shop_test:   {{ path('shop_test') }}</pre>
<pre>front_index: {{ url('front_index') }}</pre>
<pre>front_test:  {{ url('front_test') }}</pre>

当我从index.html.twig MyFrontBundle(路由example.com)运行上述代码时:

shop_index:  http://shop.example.com/app_dev.php/
shop_test:   //shop.example.com/app_dev.php/test
front_index: http://example.com/app_dev.php/
front_test:  http://example.com/app_dev.php/test

然而index.html.twig MyShopBundle(路由shop.example.com)中的相同代码会产生:

shop_index:  http://shop.example.com/app_dev.php/
shop_test:   /app_dev.php/test
front_index: http://shop.example.com/app_dev.php/     <!-- wrong! -->
front_test:  http://shop.example.com/app_dev.php/test <!-- wrong! -->

正如您所看到的那样,问题是从子域生成URL,以及域的完整或相对URL。我怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

由于前端路由未固定到商店路由等域,因此url函数使用当前域,因为每个域都匹配此路由。尝试修复你的前端路线:

example_front:
    resource: "@MyFrontBundle/Controller/"
    type:     annotation
    prefix:   /
    host:     "example.com"