("参数""对于路线""必须匹配" [^ /] ++"(""给出)使用pagerfanta

时间:2015-12-28 21:07:31

标签: symfony pagerfanta

我试图让PagerFanta工作来分页我的数据。我可以查看第一页,pagerfanta部分中生成的代码生成所有正确的链接,但是当我点击任何相应的链接时,我会在标题中获得异常。看起来pagerfanta twig函数在生成路径时没有给出正确的参数。

控制器操作:

/**
     * @Route("/people/{page}", name="view_people")
     * @Template("AppBundle:Default:people.html.twig")
     */
    public function viewPeopleAction($page)
    {
        $people = $this->getDoctrine()
            ->getRepository('AppBundle\Entity\Person')
            ->findAll();
        $adapter = new ArrayAdapter($people);
        $pager = new Pagerfanta($adapter);
        $pager->setMaxPerPage(45);

        try {
            $pager->setCurrentPage($page);
        } catch(NotValidCurrentPageException $e) {
            throw new NotFoundHttpException();
        }

        return array('pager' => $pager);
    }

Twig页面:

{% extends '::base.html.twig' %}

{% block title %}{% endblock %}

{% block body %}
    <table>
  {% for object in pager.currentPageResults %}
    <tr>
      <td><a href="{{ path('view_person', {'personid': object.id}) }}">{{ object.name }}</a></td>
    </tr>
  {% endfor %}
</table>

{% if pager.haveToPaginate %}
  {{ pagerfanta(pager, "twitter_bootstrap_translated", {"routeName": "view_people", "pageParameter": "[page]"} ) }}
{% endif %}
{% endblock body %}
{% block javascripts %}
{% endblock %}

完整错误:

An exception has been thrown during the rendering of a template ("Parameter "page" for route "view_people" must match "[^/]++" ("" given) to generate a corresponding URL.") in AppBundle:Default:people.html.twig at line 15.

500 Internal Server Error - Twig_Error_Runtime
1 linked Exception:
InvalidParameterException »

[2/2] Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Parameter "page" for route "view_people" must match "[^/]++" ("" given) to generate a corresponding URL.") in AppBundle:Default:people.html.twig at line 15.   +

[1/2] InvalidParameterException: Parameter "page" for route "view_people" must match "[^/]++" ("" given) to generate a corresponding URL.

导致此错误的代码有什么问题? 任何和所有的帮助非常感谢。

1 个答案:

答案 0 :(得分:2)

您需要为page参数设置默认值,因为当页面为1时,此参数将被省略:

/**
 * @Route("/people/{page}", name="view_people", defaults={"page"=1})
 * @Template("AppBundle:Default:people.html.twig")
 */
public function viewPeopleAction($page)
{
    // ...
}