Symfony paging implementation method, symfony paging implementation
1. The components are used for symfony paging.KnpPaginatorBundleImplement Paging
2. Download with composer
In the command line: composer require "knplabs/knp-paginator-bundle"
3. register the component in the framework and register it in app/Resources/AppKernel. php of the project.
Public functionregisterBundles (){
$ Bundles = [new Knp \ Bundle \ PaginatorBundle \ KnpPaginatorBundle (),];
}
4. Code in the Controller
Class NewsController extends Controller {/*** 2016-1-19 * auth: lsf * query list * @ param int $ page pages * @ param int $ number of lines displayed by limit */public function indexAction ($ page, $ limit) {$ em = $ this-> getDoctrine ()-> getManager (); $ qb = $ em-> getRepository ('appbundle: demolist ') -> createQueryBuilder ('U '); // Appbundle is your module. DemoList is your table entity. u is the alias. The following conditions are available: $ paginator = $ this-> get ('knp _ paginator '); $ pagination = $ paginator-> paginate ($ qb, $ page, $ limit); return $ this-> render ('news/list.html. twig ', ['pagination' => $ pagination]);}
5. Routing
News_page: path: "/news/{page}/{limit}" defaults: {_ controller: AppBundle: News: index, page: 1, limit: 2}
6. Template
{% For value in pagination % }{{ value. title }{# is the value #}{% endfor % }{{ knp_pagination_render (pagination )}}