symfony分页实现方法
1.symfony分页是要用到组件的,所以这里使用KnpPaginatorBundle实现翻页
2. 用composer下载
在命令行中: composer require "knplabs/knp-paginator-bundle"
3.需要到框架里面注册该组件在项目下的app/Resources/AppKernel.php里面注册
public functionregisterBundles() {
$bundles = [ new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(), ];
}
4.控制器中的代码
class NewsController extends Controller { /** * 2016-1-19 * auth:lsf * 查询列表 * @param int $page 页数 * @param int $limit 显示条数 */ public function indexAction($page,$limit){ $em = $this->getDoctrine()->getManager(); $qb = $em->getRepository('AppBundle:DemoList')->createQueryBuilder('u'); //Appbundle是你的模块DemoList是你的表实体 u是别名后面可接条件 $paginator = $this->get('knp_paginator'); $pagination = $paginator->paginate($qb, $page,$limit); return $this->render('news/list.html.twig',['pagination' => $pagination]); } }
5.路由
news_page:
path: "/news/{page}/{limit}"
defaults: {_controller: AppBundle:News:index,page:1,limit:2}
6.模板
{% for value in pagination %} {{value.title}}{#直接就是值了#} {% endfor %} {{ knp_pagination_render(pagination) }}