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

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

问题描述

参考此,

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

我们可以将网址格式映射到控制器和操作

app/config/routing.yml

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

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

app/config/routing.yml

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

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

例如在嫩枝中,

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

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

解决方案

您可以通过使用Symfony框架控制器之一来做到这一点,尽管我不确定如何使用参数:

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

请注意,path: /blog/{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天全站免登陆