使用 Twig 模板引擎为具有所需占位符 ( /post/{id} ) 的路由动态创建链接

Using Twig templating engine to dynamically create links for a route with a required placeholder ( /post/{id} )

所以我有一个包含 post 的数组,它被传递给 twig,Twig 将使用 for 循环从那里输出数组中的每个 post。我希望每个 post 都有一个带有 url 的删除按钮,该按钮附有 post 的 ID 所以在我的for循环中我有这样的东西:

{% for post in Posts %}
<div class="yellBox">
  <div class="col-sm-2">
    <img class="square yellBoxImage" src="https://pbs.twimg.com/profile_images/637255421099536384/dkLZc90x.jpg" alt="Avatar" />
  </div>
  <div class="yellBody col-sm-10">

    <h4><strong>{{ User.name }}</strong><span class="yellDate">- {{ post.created_at }}</span></h4>
    <p>
      {{ post.body }}
    </p>
    // other buttons

    //the delete button in question
    <a href="{{ path_for('deleteYell')}}/{{ post.id }}"><button type="button" name="button">Delete</button></a>
    <div id="test">

    </div>
  </div>
</div>
{% endfor %}

{{ path_for('deleteYell')}}/{{ post.id }} 这让我出错,说 url 末尾缺少信息,这意味着 post.id 部分在 /[= 之后没有被附加14=]

我的路线如下:

$this->delete('/yell/{id}', 'UserController:deleteYell')->setName('deleteYell');

我应该做哪些更改才能使其正常工作?

您可以将路由的参数作为第二个参数(作为数组)传递,例如:

{{ path_for('deleteYell', { 'id': post.id }) }}

如上所述here,希望对您有所帮助