8

提到这个,

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

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

应用程序/配置/路由.yml

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

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

应用程序/配置/路由.yml

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

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

例如在树枝中,

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

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

4

4 回答 4

9

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

于 2014-04-04T11:12:51.500 回答
1

从 Symfony 2.2 开始,这可以通过向路由添加主机约束来实现:

路由.yml

user_homepage:
  path: /path/to/whatever
  host: "sub.domain.ext"
  defaults: 
    _controller: forExampleAnyNamespaceBundle:Controller:action

关于这个问题有一篇官方博客文章:http: //symfony.com/blog/new-in-symfony-2-2-url-host-support-in-the-routing

于 2016-10-05T10:01:21.413 回答
-2

似乎没有原生的 Symfony 方法来处理这个问题。

于 2013-08-06T12:18:49.040 回答
-2

Symfony 的路由器功能不能那样工作......

我建议你为此创建一个 Twig 扩展。在此处阅读有关此内容的更多信息:http: //symfony.com/doc/current/cookbook/templating/twig_extension.html

您可以创建一个与常规 url() 函数非常相似的函数,因此您可以尽可能轻松地进行迁移。

{{ legacyUrl('blog_post', {slug: 'my-blog-post'}) }}

将博客迁移到 Symfony 后,您需要做的就是创建一个名为“blog_post”的路由并将“legacyUrl”更改为“url”。

于 2013-07-26T11:26:29.933 回答