简体   繁体   English

在Twig路径中将对象作为参数传递

[英]Passing object as parameter in Twig path

When I want to pass parameter via Twig path I write: 当我想通过Twig路径传递参数时,我这样写:

<a href="{{ path('blog_post', { id: post.id }) }}">

Is it possible to pass full object eg Post via Twig path? 是否可以通过Twig路径传递完整对象,例如Post?

Thank for help 谢谢帮忙

No, and in my opinion it makes no sense . 不,我认为这没有任何意义。

How would you want the object to be represented in the url ? 您希望如何在url中表示该对象? 'blog/post/{blog.id:1,blog.author:Foo,blog.title:bar}' ? 'blog/post/{blog.id:1,blog.author:Foo,blog.title:bar}'吗? What if the object change between the time the server display the page (and generate the link)and the time the user clicks on it 如果对象在服务器显示页面(并生成链接)的时间与用户单击页面的时间之间发生了变化,该怎么办?

It's pretty possible to pass a single numeric id to a controller and get the object instance of that id. 很有可能将单个数字id传递给控制器​​并获取该ID的对象实例。

Just specify the type of object of the parameter, like this: 只需指定参数的对象类型,如下所示:

/**
 * @Route("/blog/show/{blog_post}", name="blog-show")
 */
public function showAction(BlogPost $blog_post)
{
    if ($blog_post) {
        var_dump($blog_post)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM