我在我的Symfony 2.1项目中安装了KnpPaginatorBundle,并配置了分页器。
使用分页器后,我的URL看起来像:
http://dev.localhost/app_dev.php/news/development?page=3
是否可以将URL更改为像这样(或类似的 - 没有?和=字符)?
http://dev.localhost/app_dev.php/news/development/page/3
我在我的Symfony 2.1项目中安装了KnpPaginatorBundle,并配置了分页器。
使用分页器后,我的URL看起来像:
http://dev.localhost/app_dev.php/news/development?page=3
是否可以将URL更改为像这样(或类似的 - 没有?和=字符)?
http://dev.localhost/app_dev.php/news/development/page/3
news_development_route:
pattern: /news/development/{page}
defaults: {_controller: AcmeMainBundle:Article:list, page: 1 }
// 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));
}
/**
* @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');
}
... 转换为以下形式(请参阅“页面”参数):
/**
* @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');
}
@Route("/list/{name}", name="_article_list")
),则可以使用$pagination->addParam('name', 'Finance');
将它们添加进去。我仍在寻找如何以干净的 URL 开始(因此取消所有当前查询字符串参数)。 - rolandow