如何在 symfony 2 中创建映射到外部 URL 的路由? [英] How to create the route in symfony 2 which maps to external URL?

查看:21
本文介绍了如何在 symfony 2 中创建映射到外部 URL 的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提到这个,

http://symfony.com/doc/current/book/routing.html

我们可以将 url 模式映射到控制器和动作

app/config/routing.yml

blog_show:路径:/博客/{slug}默认值:{ _controller: AcmeBlogBu​​ndle:Blog:show }

我想将路径映射到外部 url.

app/config/routing.yml

blog_show:路径:/博客/{slug}默认值:www.example.com/blog"

要求是,我当前的网站在kohana,我正在逐步将其移植到symfony 2.对于我的symfony2应用程序kohana URL就像外部URL,我想在路由中配置这些URL并以标准方式使用它们,

例如在树枝中,

阅读这篇博文.</a>

所以稍后当我将我的页面移植到 Symfony 时,我将只需要更改路由文件,以便我可以使用相同的 blog_show 键来引用 url,而且我不必更改我使用过 url 的所有文件.

您可以通过使用 Symfony 框架控制器之一来完成此操作,尽管我不确定这将如何与参数一起使用:

blog_show:路径:/博客/{slug}默认值:_controller: FrameworkBundle:Redirect:urlRedirect路径:http://example.com/blog"永久:真实

注意path:/blog/{slug}是直接抓取slug,但是path:"http://example.com/blog/{slug}"不起作用.

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

Referring to this,

http://symfony.com/doc/current/book/routing.html

we can map url pattern to controller and action

app/config/routing.yml

blog_show:
    path:      /blog/{slug}
    defaults:  { _controller: AcmeBlogBundle:Blog:show }

I want to map the path to external url.

app/config/routing.yml

blog_show:
    path:      /blog/{slug}
    defaults:  "www.example.com/blog"

The requirement is, my current website is in kohana, I am porting it gradually to symfony 2. For my symfony2 app kohana URL are like external urls, I want to configure these urls in routing and use them in standard way,

e.g. in Twig,

<a href="{{ path('blog_show'}}">
  Read this blog post.
</a>

So later on when I port my pages to Symfony, I will have to change only routing file so that I could use same blog_show key to refer to url and I wont' have to change all the files where I have used urls.

解决方案

You can do this by using one of the Symfony framework controllers although I'm not sure how this would work with parameters:

blog_show:
    path: /blog/{slug}
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: "http://example.com/blog"
        permanent: true

Note that path: /blog/{slug} grabs the slug directly, but path: "http://example.com/blog/{slug}" doesn't work.

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

这篇关于如何在 symfony 2 中创建映射到外部 URL 的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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