Is it possible to create a route that points to an absolute external URL?

左心房为你撑大大i 提交于 2019-12-11 03:34:26

问题


Google, as well as Symfony Docs and Cookbook haven't helped.

Let's say we have a website called http://www.example.com that is running on Symfony2 and we are using an external FAQ and Support Service (for whatever reason) located at http://example.externalservice.com.

We now thought it would be a suitable solution to create a route that points to the external FAQ service which would result in something like that:

# ../config/routing.yml
[...]

info.faq:
    url: http://example.externalservice.com     # Surely isn't working

[...]

This, because there could be a need to replace the support service with a home-brewed solution located within the application in the medium term. This route is used on various pages, so hardcoding or replacing them all when the support service changes is not an option.

So, my questions are:

  • Is this the right approach or should we be using a TWIG extension and a parmeter located either in parameters.yml or config.yml?
  • If it is the right approach, is it possible to create such a route without modifying the routing component itself?

Thank you in advance.


回答1:


In your parameters.yml add

external_url: http://example.externalservice.com 

then in your controller :

$url = $this->container->getParameter('external_url');
return $this->redirect($url);



回答2:


From the Symfony2 documentation:

root:
    path:  /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: http://www.google.fr
        permanent: true

For more informations about redirection, read the documentation below:

How to configure a redirect to another route without a custom controller



来源:https://stackoverflow.com/questions/17214438/is-it-possible-to-create-a-route-that-points-to-an-absolute-external-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!