【问题标题】:Bug in generating urls from a subdomains in Symfony 2?从 Symfony 2 中的子域生成 url 的错误?
【发布时间】:2014-03-08 14:57:21
【问题描述】:

我的项目有一个子域 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:   /

URL 匹配工作正常,但我有一些生成完整/相对 url 的问题(将域链接到子域,反之亦然):

<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.twigMyFrontBundle(路由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.twigMyShopBundle(路由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。我该如何解决这个问题?

【问题讨论】:

    标签: symfony routing symfony-2.3 symfony-routing


    【解决方案1】:

    由于您的前端路线不像商店路线那样固定到域,url 函数使用当前域,因为每个域都与此路由匹配。尝试修复您的前端路线:

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

    【讨论】:

    • 现在完美运行!谢谢!任何机会使域动态?我在 parameters.yml 中使用了一个固定参数“域”,但我想使用一个容器(服务器名称)。找不到...
    猜你喜欢
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2012-01-15
    相关资源
    最近更新 更多