Home  >  Article  >  Backend Development  >  How to implement paging using symfony

How to implement paging using symfony

一个新手
一个新手Original
2017-10-10 09:06:402090browse

1. Symfony paging requires components, so KnpPaginatorBundle is used here to achieve page turning

2. Use composer to download

In the command line: composer require "knplabs/knp-paginator-bundle"

3. You need to register the component in the framework and register it in app/Resources/AppKernel.php under the project

 public functionregisterBundles() {   
            $bundles = [                     
            new   Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),         ];    
         }

4. In the controller Code


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. Routing

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) }}

The above is the detailed content of How to implement paging using symfony. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn