0

所以我正在尝试扩展 SonataAdmin 显示视图以获取文档版本列表(phpcr 检查点或签入)。

我已经正确显示了已保存版本的列表,现在我需要将它们制作成显示该内容版本的链接,但是在尝试添加自定义路由时出现以下错误:

An exception has been thrown during the rendering of a template 
("Parameter "id" for route "admin_cmsbundle_product_show_version" must 
match "[^/]++" ("mstr/product/product-253562" given) to generate a 
corresponding URL.") in CmsBundle:product:show.html.twig at line 18.

这是我的管理类中的 configureRoutes:

protected function configureRoutes(RouteCollection $collection)
{
    $collection->add('show_version', $this->getRouterIdParameter() . '/show/{version}');
}

这是我的覆盖模板:

{% block show %}

<ul>
    {% for version in versions %}
        <li><a href="{{  admin.generateObjectUrl('show_version', object, {'version': version.name}) }}">Version: {{ version.name }} </a></li>
    {% endfor %}
</ul>

{{ parent() }}    

{% 端块 %}

这是我编辑的显示操作(包括版本列表):

public function showAction($id = null)
{ 
    ...
    return $this->render($this->admin->getTemplate('show'), array(
        'action'   => 'show',
        'object'   => $object,
        'elements' => $this->admin->getShow(),
        'versions' => $this->getVersionHistory($object)
    ));
}

这是我在控制器中的 showVersion 操作:

public function showVersionAction($id = null, $version = "1.0")
{ 
    ...
    return $this->render($this->admin->getTemplate('show'), array(
        'action'   => 'show',
        'object'   => $this->getVersion($object, $version),
        'elements' => $this->admin->getShow(),
        'versions' => $this->getVersionHistory($object)
    ));
}

注意, generateUrl 给出了同样的错误:

<a href="{{  admin.generateUrl('show_version', {'id': object.id, 'version': version.name}) }}">Version: {{ version.name }} </a>

我究竟做错了什么?

任何有关解决此问题的帮助将不胜感激:)

4

2 回答 2

0

根据错误消息,存在一些问题$object。所以也许使用generateUrl而不是generateObjectUrl()并在数组中传递您的 id:

{% block show %}

<ul>
    {% for version in versions %}
        <li><a href="{{  admin.generateUrl('show_version', {'id':object.id,'version': version.name}) }}">Version: {{ version.name }} </a></li>
    {% endfor %}
</ul>

{{ parent() }}
{% endblock %}
于 2014-12-10T03:09:32.103 回答
0

一点点挖掘,答案很简单,只需将模式覆盖为 id 为 .+

protected function configureRoutes(RouteCollection $collection)
{
    $collection->add('show_version', $this->getRouterIdParameter() . '/show/{version}', array(), array('id' => '.+'));
}
于 2014-12-10T09:32:16.870 回答