Home > Backend Development > PHP Tutorial > How to implement paging using symfony

How to implement paging using symfony

一个新手
Release: 2023-03-16 14:58:01
Original
2127 people have browsed it

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(),         ];    
         }
Copy after login

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]);
    }
}
Copy after login

5. Routing

news_page:
  path: "/news/{page}/{limit}"
  defaults: {_controller: AppBundle:News:index,page:1,limit:2}
Copy after login


6.模板
{% for value in pagination %}
{{value.title}}{#直接就是值了#}
{% endfor %}
{{ knp_pagination_render(pagination) }}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template