(“参数“"路由“"必须匹配“[^/]++"(“"给定)使用pagerfanta [英] ("Parameter "" for route "" must match "[^/]++" ("" given) using pagerfanta
问题描述
我正在尝试让 PagerFanta 工作来分页我的数据.我可以查看第一页,并且 pagerfanta
部分中生成的代码会生成所有正确的链接,但是当我单击这些相应链接中的任何一个时,标题中出现异常.似乎 pagerfanta
twig 函数在生成路由时没有给出正确的参数.
I'm attempting to get PagerFanta working to page my data. I can view the first page and the generated code in the pagerfanta
portion generates all the correct links, however when I click on any of those corresponding links I get the exception in the title. It seems like the pagerfanta
twig function is not giving the correct parameter when generating the routes.
控制器动作:
/**
* @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);
}
树枝页:
{% 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.
我的代码有什么问题导致了这个错误?非常感谢任何和所有帮助.
What is wrong with my code that is causing this error? Any and all help greatly appreciated.
推荐答案
page
参数需要设置一个默认值,当页面为一页时会省略这个参数:
You need to set a default value for the page
parameter as this one will be omitted when the page is one:
/**
* @Route("/people/{page}", name="view_people", defaults={"page"=1})
* @Template("AppBundle:Default:people.html.twig")
*/
public function viewPeopleAction($page)
{
// ...
}
这篇关于(“参数“"路由“"必须匹配“[^/]++"(“"给定)使用pagerfanta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!