【问题标题】:Symfony2 routing.yml to external urlSymfony2 routing.yml 到外部 url
【发布时间】:2014-06-23 15:12:01
【问题描述】:

我的问题是:如何从 routing.yml 生成外部 url?

我想要类似的东西:

documentation_product1:
    pattern:  /my/documentation/product1.php
    default:  http://www.myothersite.com/product1

有没有可能或类似的东西?

【问题讨论】:

标签: php symfony


【解决方案1】:

可以在控制器内重定向到外部站点

documentation_product1:
pattern:  /my/documentation/product1.php
defaults:  { _controller: AcmeBundle:Product:show }

// in controller
$this->redirect('http://www.myothersite.com/product1');

但这不是一个好习惯。

最好使用 FrameworkBundle 中的 RedirectController。(由 Paulpro 推荐)

独库:

http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html

https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

【讨论】:

  • 如果整个目的是做一个重定向,你可以只使用FrameworkBundle中现有的RedirectController。
  • 谢谢,不知道控制器:)
  • 这似乎是两个答案中更好的一个,因为接受的答案似乎没有正确回答问题。
【解决方案2】:

Symfony 将请求的 url 路由到一对控制器和要执行的操作。它只做内部重定向。

要重定向到外部 URL,您需要 .htaccess 和 mod_rewrite(如果您使用 apache)。

【讨论】:

  • 或者其他webserver相关的指令,取决于web server,如果不使用apache的话
  • 这不是真的(不再)。可以仅通过路由配置重定向到外部 URL,请参见此处:stackoverflow.com/a/17469952/283097
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多