我在Symfony 2.1项目中安装了KnpPaginatorBundle,还配置了Paginator。
使用分页,我的URL如下所示:
http://dev.localhost/app_dev.php/news/development?page=3有没有可能把URL改成这样(或者类似的--没有?and =字符)?
http://dev.localhost/app_dev.php/news/development/page/3发布于 2014-11-25 18:02:15
我找到了解决方案,要解决你的问题,请遵循以下步骤:
1)创建新路由或更改旧路由,因此在routing.yml中添加以下内容:
news_development_route:
pattern: /news/development/{page}
defaults: {_controller: AcmeMainBundle:Article:list, page: 1 }2)在你的类控制器中,像这样改变你的方法:
// Acme\MainBundle\Controller\ArticleController.php
public function listAction($page)/*add the $page param*/
{
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM AcmeMainBundle:Article a";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', $page)/*change the number 1 by the $page parameter*/,
10/*limit per page*/
);
$pagination->setUsedRoute('news_development_route'); /*define the pagination route*/
// parameters to template
return $this->render('AcmeMainBundle:Article:list.html.twig', array('pagination' => $pagination));
}就这样
发布于 2012-12-14 06:51:32
在您的控制器中,您需要更改以下内容:
/**
* @Route("/list/", name="_user_list")
* @Template()
*/
public function listAction()
{
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM HPPTarjetaBundle:User a";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
10/*limit per page*/
);
return compact('pagination');
}..。(参见"page“参数):
/**
* @Route("/list/{page}", name="_user_list")
* @Template()
*/
public function listAction($page)
{
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM HPPTarjetaBundle:User a";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$page/*page number*/,
10/*limit per page*/
);
return compact('pagination');
}https://stackoverflow.com/questions/12572301
复制相似问题