【问题标题】:Symfony2 routing error (parameter <x> for route <r> must match <pattern>)Symfony2 路由错误(路由 <r> 的参数 <x> 必须匹配 <pattern>)
【发布时间】:2014-04-28 15:49:32
【问题描述】:

我是 Symfony2 框架的新手。这个错误需要我永远解决。我根据 Jobeet 教程构建了自己的项目。现在我已经成功生成 CRUD,但是在打开索引页面时,我遇到了这样的路由错误:

在渲染模板期间引发了异常 (“路由“sifo_mst_periode_show”的参数“id”必须匹配“[^/]++” ("" given) 生成对应的 URL。") 在 SifoSchoolBundle:MstPeriode:index.html.twig 在第 21 行。

这是我的 index.html.twig 第 21 行:

<td><a href="{{ path('sifo_mst_periode_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>

这是路线:

sifo_mst_periode:
    pattern:  /
    defaults: { _controller: "SifoSchoolBundle:MstPeriode:index" }

sifo_mst_periode_show:
    pattern:  /{id}/show
    defaults: { _controller: "SifoSchoolBundle:MstPeriode:show" }

这是路由:

SifoSchoolBundle_sifo_mst_periode:
    resource: "@SifoSchoolBundle/Resources/config/routing/mstperiode.yml"
    prefix:   /periode

【问题讨论】:

  • 确保entity.id 有一定的价值
  • 如何初始化entity?如果它在循环中,请确保检查 entity.id 是否为空或 null!
  • @PCoder : Atually im using generate:crud command based Jobeet tutorial day 3 intelligentbee.com/blog/2013/08/09/… 它是自动生成的。
  • 如何放置一个if条件来检查它是空还是空?
  • @PCoder :它的空值。似乎自动生成的表格没有将PK放入表格中。通常在 PHP 中,我会手动输入主键。如果这超出了主题,我很抱歉。但是我必须将所有PK更改为自动吗?因此它将在没有用户交互的情况下生成,而是将新字段作为用户界面的代码。

标签: php symfony


【解决方案1】:

这个案子已经解决了。问题是主键 (entity.id) 的 NULL 值未显示在添加表单中。我将所有主键设置为自动,而不是由用户表单处理。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多