友好的 Symfony 分页器可对所有内容进行分页
一般来说,这个包是基于 Knp Pager 组件的。该组件引入了一种不同的分页处理方式。您可以在给定的文档链接上阅读有关内部逻辑的更多信息。
注意:保持knp-components与此包同步。如果您想使用旧版本的 KnpPaginatorBundle - 在存储库中使用v3.0或v4.X标签,适合对ODM MongoDB和ORM 2.0查询进行分页
有关更改的详细信息,请阅读有关版本的信息。
>=4.4
。>=6.4
版本。>=3.0
版本。 注意:使用多个分页器需要设置别名以保持参数不冲突。
composer require knplabs/knp-paginator-bundle
如果你不使用flex(你应该使用),你需要手动启用bundle:
// app / AppKernel . php
public function registerBundles ()
{
return [
// ...
new Knp Bundle PaginatorBundle KnpPaginatorBundle (),
// ...
];
}
您可以配置默认查询参数名称和模板,以及一些其他选项:
knp_paginator :
convert_exception : false # throw a 404 exception when an invalid page is requested
page_range : 5 # number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
remove_first_page_param : false # remove the page query parameter from the first page link
default_options :
page_name : page # page query parameter name
sort_field_name : sort # sort field query parameter name
sort_direction_name : direction # sort direction query parameter name
distinct : true # ensure distinct results, useful when ORM queries are using GROUP BY statements
filter_field_name : filterField # filter field query parameter name
filter_value_name : filterValue # filter value query parameter name
page_out_of_range : ignore # ignore, fix, or throwException when the page is out of range
default_limit : 10 # default number of items per page
template :
pagination : ' @KnpPaginator/Pagination/sliding.html.twig ' # sliding pagination controls template
rel_links : ' @KnpPaginator/Pagination/rel_links.html.twig ' # <link rel=...> tags template
sortable : ' @KnpPaginator/Pagination/sortable_link.html.twig ' # sort link template
filtration : ' @KnpPaginator/Pagination/filtration.html.twig ' # filters template
// config / packages / paginator . php
<?php declare (strict_types= 1 );
use Symfony Component DependencyInjection Loader Configurator ContainerConfigurator ;
return static function ( ContainerConfigurator $ configurator ): void
{
$ configurator -> extension ( ' knp_paginator ' , [
' convert_exception ' => false , // throw a 404 exception when an invalid page is requested
' page_range ' => 5 , // number of links shown in the pagination menu ( e . g : you have 10 pages , a page_range of 3 , on the 5 th page you ' ll see links
' remove_first_page_param ' => false , // remove the page query parameter from the first page link
' default_options ' => [
' page_name ' => ' page ' , // page query parameter name
' sort_field_name ' => ' sort ' , // sort field query parameter name
' sort_direction_name ' => ' direction ' , // sort direction query parameter name
' distinct ' => true , // ensure distinct results , useful when ORM queries are using GROUP BY statements
' filter_field_name ' => ' filterField ' , // filter field query parameter name
' filter_value_name ' => ' filterValue ' // filter value query parameter name
'page_out_of_range' => 'ignore', // ignore , fix , or throwException when the page is out of range
' default_limit ' => 10 // default number of items per page
],
' template ' => [
' pagination ' => ' @KnpPaginator/Pagination/sliding.html.twig ' , // sliding pagination controls template
' rel_links ' => ' @KnpPaginator/Pagination/rel_links.html.twig ' , // < link rel = ... > tags template
' sortable ' => ' @KnpPaginator/Pagination/sortable_link.html.twig ' , // sort link template
' filtration ' => ' @KnpPaginator/Pagination/filtration.html.twig ' // filters template
]
]);
};
这可以在knp_paginator.template.pagination
键中开箱即用:
@KnpPaginator/Pagination/sliding.html.twig
(默认情况下)@KnpPaginator/Pagination/bootstrap_v5_pagination.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v3_pagination.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_pagination.html.twig
@KnpPaginator/Pagination/foundation_v6_pagination.html.twig
@KnpPaginator/Pagination/foundation_v5_pagination.html.twig
@KnpPaginator/Pagination/bulma_pagination.html.twig
@KnpPaginator/Pagination/semantic_ui_pagination.html.twig
@KnpPaginator/Pagination/materialize_pagination.html.twig
@KnpPaginator/Pagination/tailwindcss_pagination.html.twig
@KnpPaginator/Pagination/uikit_v3_pagination.html.twig
这可以在knp_paginator.template.rel_links
键中开箱即用:
@KnpPaginator/Pagination/rel_links.html.twig
(默认情况下) 这可以在knp_paginator.template.sortable
键中开箱即用:
@KnpPaginator/Pagination/sortable_link.html.twig
(默认情况下)@KnpPaginator/Pagination/bootstrap_v5_bi_sortable_link.html.twig
@KnpPaginator/Pagination/bootstrap_v5_fa_sortable_link.html.twig
@KnpPaginator/Pagination/bootstrap_v5_md_sortable_link.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v3_sortable_link.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_font_awesome_sortable_link.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_material_design_icons_sortable_link.html.twig
@KnpPaginator/Pagination/semantic_ui_sortable_link.html.twig
@KnpPaginator/Pagination/uikit_v3_sortable.html.twig
这可以在knp_paginator.template.filtration
密钥中开箱即用:
@KnpPaginator/Pagination/filtration.html.twig
(默认)@KnpPaginator/Pagination/bootstrap_v5_filtration.html.twig
@KnpPaginator/Pagination/twitter_bootstrap_v4_filtration.html.twig
目前分页器可以分页:
array
DoctrineDBALQueryQueryBuilder
DoctrineORMQuery
DoctrineORMQueryBuilder
DoctrineODMMongoDBQueryQuery
DoctrineODMMongoDBQueryBuilder
DoctrineODMPHPCRQueryQuery
DoctrineODMPHPCRQueryBuilderQueryBuilder
DoctrineCommonCollectionArrayCollection
- 任何 Doctrine 关系集合,包括ModelCriteria
- 推进 ORM 查询Solarium_Client
和Solarium_Query_Select
作为元素的数组 // App Controller ArticleController . php
public function listAction ( EntityManagerInterface $ em , PaginatorInterface $ paginator , Request $ request )
{
$ dql = " SELECT a FROM AcmeMainBundle:Article a " ;
$ query = $ em -> createQuery ( $ dql );
$ pagination = $ paginator -> paginate (
$ query , / * query NOT result * /
$ request -> query -> getInt ( ' page ' , 1 ), / * page number * /
10 / * limit per page * /
);
// parameters to template
return $ this -> render ( ' article/list.html.twig ' , [ ' pagination ' => $ pagination ]);
}
<head>
中: {# rel links for pagination #}
{{ knp_pagination_rel_links( pagination ) }}
<body>
中: {# total items count #}
< div class = " count " >
{{ pagination . getTotalItemCount }}
</ div >
< table >
< tr >
{# sorting of properties based on query components #}
< th >{{ knp_pagination_sortable( pagination , ' Id ' , ' a.id ' ) }}</ th >
< th {% if pagination. isSorted ( ' a.title ' ) %} class = " sorted " {% endif %}>
{{ knp_pagination_sortable( pagination , ' Title ' , ' a.title ' ) }}
</ th >
< th {% if pagination. isSorted ([ ' a.date ' , ' a.time ' ]) %} class = " sorted " {% endif %}>
{{ knp_pagination_sortable( pagination , ' Release ' , [ ' a.date ' , ' a.time ' ]) }}
</ th >
</ tr >
{# table body #}
{% for article in pagination %}
< tr {% if loop . index is odd %} class = " color " {% endif %}>
< td >{{ article . id }}</ td >
< td >{{ article . title }}</ td >
< td >{{ article . date | date ( ' Y-m-d ' ) }}, {{ article . time | date ( ' H:i:s ' ) }}</ td >
</ tr >
{% endfor %}
</ table >
{# display navigation #}
< div class = " navigation " >
{{ knp_pagination_render( pagination ) }}
</ div >
用于翻译以下文本:
%foo% name
,带有翻译键table_header_name
。翻译是在域messages
中。{0} No author|{1} Author|[2,Inf] Authors
具有翻译键table_header_author
的作者。翻译是在域messages
中。TranslationCount 和 TranslationParameters 可以组合。
< table >
< tr >
{# sorting of properties based on query components #}
< th >{{ knp_pagination_sortable( pagination , ' Id ' | trans({ foo : ' bar ' }, ' messages ' ), ' a.id ' ) | raw }}</ th >
< th {% if pagination. isSorted ( ' a.Title ' ) %} class = " sorted " {% endif %}>{{ knp_pagination_sortable( pagination , ' Title ' , ' a.title ' ) | raw }}</ th >
< th >{{ knp_pagination_sortable( pagination , ' Author ' | trans({}, ' messages ' ), ' a.author ' ) | raw }}</ th >
</ tr >
<!-- Content of the table -->
</ table >
您还可以通过以下名称格式创建翻译文件来覆盖翻译: domain.locale.format
。因此,要为此包创建翻译文件,您需要在project_root/translations/
下创建例如KnpPaginatorBundle.tr.yaml
文件,并在其中添加翻译:
label_previous : " Önceki "
label_next : " Sonraki "
filter_searchword : " Arama kelimesi "
如果您相应地设置配置的默认翻译:
framework :
default_locale : tr
Symfony 会自动选择它。
您可以使用knp_paginator.injectable
DIC 标记将分页器服务自动注入到另一个服务中。该标签采用一个可选参数paginator
,它是应注入的分页器服务的 ID。它默认为knp_paginator
。
接收 KnpPaginator 服务的类必须实现KnpBundlePaginatorBundleDefinitionPaginatorAwareInterface
。如果你太懒了,你也可以扩展KnpBundlePaginatorBundleDefinitionPaginatorAware
基类。
不鼓励使用
PaginatorAwareInterface
发出警告,并且可能会在未来版本中删除。您不应该依赖 setter 注入,而只能依赖正确的构造函数注入。使用 Symfony 内置的自动装配机制是建议的方法。
如果安装了symfony/proxy-manager-bridge
包, knp_paginator
服务将被延迟创建。
有关惰性服务的更多信息,请参阅有关依赖注入的 Symfony 文档。
<? xml version = " 1.0 " ?>
< container xmlns = " http://symfony.com/schema/dic/services "
xmlns : xsi = " http://www.w3.org/2001/XMLSchema-instance "
xsi : schemaLocation = " http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd " >
< parameters >
< parameter key = " my_bundle.paginator_aware.class " >MyBundleRepositoryPaginatorAwareRepository</ parameter >
</ parameters >
< services >
< service id = " my_bundle.paginator_aware " class = " my_bundle.paginator_aware.class " >
< tag name = " knp_paginator.injectable " paginator = " knp_paginator " />
</ service >
</ services >
</ container >
framework :
translator : { fallbacks: ['%locale%'] }
translations/KnpPaginatorBundle.en.yml
中创建您自己的翻译文件(如果需要,请将“en”替换为您自己的语言代码)。然后添加这些行: label_next : Next
label_previous : Previous
<rel>
链接仅在使用分页时才有意义,它们与排序或过滤无关。 请先阅读这篇文章。
该库由以下人员维护(按字母顺序排列):