小编Zlo*_*roh的帖子

symfony 2按模型路由

例如Sf2.0,标准博客.

routing.yml代码:

DevBlogBundle_post_show:
 pattern:  /posts/{id}
 defaults: { _controller: "DevBlogBundle:Post:show" }
 requirements:
    _method:  GET
    id: \d+
Run Code Online (Sandbox Code Playgroud)

标准方式我使用以下方法为我的帖子生成网址:

path('DevBlogBundle_post_show',{'id':post.id})
Run Code Online (Sandbox Code Playgroud)

我在所有temlates /布局中使用此构造,其中包括帖子列表.如果我想改变我的post_show路线(比如说...添加Slug参数/posts/{id}.{slug}),我将需要更改我的所有temlates.相反,我想通过我的Post模型生成路线,例如:

public function getUrl(){
     return $this->generator->generate('DevBlogBundle_post_show',array (...params...));}
Run Code Online (Sandbox Code Playgroud)

问题:如何将此生成器发送到我的Post模型,我必须"使用..."以及如何生成路径?

在我的模板中,我想放置:

<a href="{{ post.getUrl() }}" ...>...</a>
Run Code Online (Sandbox Code Playgroud)

提前致谢.

php routing symfony

2
推荐指数
1
解决办法
2612
查看次数

标签 统计

php ×1

routing ×1

symfony ×1