{"id":13624219,"url":"https://github.com/whiteoctober/Pagerfanta","last_synced_at":"2025-04-15T20:33:39.712Z","repository":{"id":1437851,"uuid":"1666651","full_name":"whiteoctober/Pagerfanta","owner":"whiteoctober","description":"Pagination for PHP.","archived":false,"fork":true,"pushed_at":"2020-06-09T11:57:17.000Z","size":370,"stargazers_count":1592,"open_issues_count":35,"forks_count":2,"subscribers_count":40,"default_branch":"master","last_synced_at":"2024-11-08T12:43:14.714Z","etag":null,"topics":["pagerfanta","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"BabDev/Pagerfanta","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whiteoctober.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"code_of_conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-04-26T18:55:28.000Z","updated_at":"2024-10-29T06:30:34.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/whiteoctober/Pagerfanta","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whiteoctober%2FPagerfanta","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whiteoctober%2FPagerfanta/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whiteoctober%2FPagerfanta/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whiteoctober%2FPagerfanta/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whiteoctober","download_url":"https://codeload.github.com/whiteoctober/Pagerfanta/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249148463,"owners_count":21220540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pagerfanta","php"],"created_at":"2024-08-01T21:01:40.169Z","updated_at":"2025-04-15T20:33:39.297Z","avatar_url":"https://github.com/whiteoctober.png","language":"PHP","funding_links":[],"categories":["Configuration","杂项","杂项 Miscellaneous","配置 Configuration","Miscellaneous","Table of Contents"],"sub_categories":["Miscellaneous","杂项 Miscellaneous","Globalization"],"readme":"**NB** This project is no longer maintained; you may like to use https://github.com/BabDev/Pagerfanta instead.\n\n# Pagerfanta\n\n[![Build Status](https://travis-ci.org/whiteoctober/Pagerfanta.png?branch=master)](https://travis-ci.org/whiteoctober/Pagerfanta) [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/whiteoctober/Pagerfanta/badges/quality-score.png?s=1ee480491644c07812b5206cf07d33a5035d0118)](https://scrutinizer-ci.com/g/whiteoctober/Pagerfanta/) [![Code Coverage](https://scrutinizer-ci.com/g/whiteoctober/Pagerfanta/badges/coverage.png?s=284be0616a9ba0439ee1123bcaf5fb3f6bfb0e50)](https://scrutinizer-ci.com/g/whiteoctober/Pagerfanta/) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/9e710230-b088-4904-baef-5f5e2d62e681/mini.png)](https://insight.sensiolabs.com/projects/9e710230-b088-4904-baef-5f5e2d62e681) [![Latest Stable Version](https://poser.pugx.org/pagerfanta/pagerfanta/v/stable.png)](https://packagist.org/packages/pagerfanta/pagerfanta) [![Total Downloads](https://poser.pugx.org/pagerfanta/pagerfanta/downloads.png)](https://packagist.org/packages/pagerfanta/pagerfanta)\n\nThis project is for PHP 7.\nIf you need support for PHP \u003c 7, use [Release v1.1.0](https://github.com/whiteoctober/Pagerfanta/releases/tag/v1.1.0).\n\n## Usage\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\ArrayAdapter;\nuse Pagerfanta\\Pagerfanta;\n\n$adapter = new ArrayAdapter($array);\n$pagerfanta = new Pagerfanta($adapter);\n\n$pagerfanta-\u003esetMaxPerPage($maxPerPage); // 10 by default\n$maxPerPage = $pagerfanta-\u003egetMaxPerPage();\n\n$pagerfanta-\u003esetCurrentPage($currentPage); // 1 by default\n$currentPage = $pagerfanta-\u003egetCurrentPage();\n\n$nbResults = $pagerfanta-\u003egetNbResults();\n$currentPageResults = $pagerfanta-\u003egetCurrentPageResults();\n```\n\nSome of the other methods available:\n\n```php\n$pagerfanta-\u003egetNbPages();\n$pagerfanta-\u003ehaveToPaginate(); // whether the number of results is higher than the max per page\n$pagerfanta-\u003ehasPreviousPage();\n$pagerfanta-\u003egetPreviousPage();\n$pagerfanta-\u003ehasNextPage();\n$pagerfanta-\u003egetNextPage();\n$pagerfanta-\u003egetCurrentPageOffsetStart();\n$pagerfanta-\u003egetCurrentPageOffsetEnd();\n```\n\n### Changing the page based on user selection\n\nIf you're using the example route-generator function shown below,\nthe page selected by the user will be available in the `page` GET (querystring) parameter.\n\nYou would then need to call `setCurrentPage` with the value of that parameter:\n\n```php\nif (isset($_GET[\"page\"])) {\n    $pagerfanta-\u003esetCurrentPage($_GET[\"page\"]);\n}\n```\n\n### setMaxPerPage and setCurrentPage\n\nThe `-\u003esetMaxPerPage()` and `-\u003esetCurrentPage()` methods implement\na fluent interface:\n\n```php\n\u003c?php\n\n$pagerfanta\n    -\u003esetMaxPerPage($maxPerPage)\n    -\u003esetCurrentPage($currentPage);\n```\n\nThe `-\u003esetMaxPerPage()` method throws an exception if the max per page\nis not valid:\n\n  * `Pagerfanta\\Exception\\NotIntegerMaxPerPageException`\n  * `Pagerfanta\\Exception\\LessThan1MaxPerPageException`\n\nBoth extend from `Pagerfanta\\Exception\\NotValidMaxPerPageException`.\n\nThe `-\u003esetCurrentPage()` method throws an exception if the page is not valid:\n\n  * `Pagerfanta\\Exception\\NotIntegerCurrentPageException`\n  * `Pagerfanta\\Exception\\LessThan1CurrentPageException`\n  * `Pagerfanta\\Exception\\OutOfRangeCurrentPageException`\n\nAll of them extend from `Pagerfanta\\Exception\\NotValidCurrentPageException`.\n\n`-\u003esetCurrentPage()` throws an out ot range exception depending on the\nmax per page, so if you are going to modify the max per page, you should do it\nbefore setting the current page.\n\n(If you want to use Pagerfanta in a Symfony project, see\n[https://github.com/whiteoctober/WhiteOctoberPagerfantaBundle](https://github.com/whiteoctober/WhiteOctoberPagerfantaBundle).)\n\n## Adapters\n\nThe adapter's concept is very simple. An adapter just returns the number\nof results and an slice for a offset and length. This way you can adapt\na pagerfanta to paginate any kind results simply by creating an adapter.\n\nAn adapter must implement the `Pagerfanta\\Adapter\\AdapterInterface`\ninterface, which has these two methods:\n\n```php\n\u003c?php\n\n/**\n * Returns the number of results.\n *\n * @return integer The number of results.\n */\nfunction getNbResults();\n\n/**\n * Returns an slice of the results.\n *\n * @param integer $offset The offset.\n * @param integer $length The length.\n *\n * @return array|\\Iterator|\\IteratorAggregate The slice.\n */\nfunction getSlice($offset, $length);\n```\n\nPagerfanta comes with these adapters:\n\n### ArrayAdapter\n\nTo paginate an array.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\ArrayAdapter;\n\n$adapter = new ArrayAdapter($array);\n```\n\n### MongoAdapter\n\nTo paginate [Mongo](http://php.net/manual/en/book.mongo.php) Cursors.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\MongoAdapter;\n\n$cursor = $collection-\u003efind();\n$adapter = new MongoAdapter($cursor);\n```\n\n### MandangoAdapter\n\nTo paginate [Mandango](http://mandango.org) Queries.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\MandangoAdapter;\n\n$query = $mandango-\u003egetRepository('Model\\Article')-\u003ecreateQuery();\n$adapter = new MandangoAdapter($query);\n```\n\n### DoctrineDbalAdapter\n\nTo paginate [DoctrineDbal](http://www.doctrine-project.org/projects/dbal.html)\nquery builders.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineDbalAdapter;\nuse Doctrine\\DBAL\\Query\\QueryBuilder;\n\n$queryBuilder = new QueryBuilder($conn);\n$queryBuilder-\u003eselect('p.*')-\u003efrom('posts', 'p');\n\n$countQueryBuilderModifier = function ($queryBuilder) {\n    $queryBuilder-\u003eselect('COUNT(DISTINCT p.id) AS total_results')\n          -\u003esetMaxResults(1);\n};\n\n$adapter = new DoctrineDbalAdapter($queryBuilder, $countQueryBuilderModifier);\n```\n\n### DoctrineDbalSingleTableAdapter\n\nTo simplify the pagination of single table\n[DoctrineDbal](http://www.doctrine-project.org/projects/dbal.html)\nquery builders.\n\nThis adapter only paginates single table query builders, without joins.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineDbalSingleTableAdapter;\nuse Doctrine\\DBAL\\Query\\QueryBuilder;\n\n$queryBuilder = new QueryBuilder($conn);\n$queryBuilder-\u003eselect('p.*')-\u003efrom('posts', 'p');\n\n$countField = 'p.id';\n\n$adapter = new DoctrineDbalSingleTableAdapter($queryBuilder, $countField);\n```\n\n### DoctrineORMAdapter\n\nTo paginate [DoctrineORM](http://www.doctrine-project.org/projects/orm) query objects.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineORMAdapter;\n\n$queryBuilder = $entityManager-\u003ecreateQueryBuilder()\n    -\u003eselect('u')\n    -\u003efrom('Model\\Article', 'u');\n$adapter = new DoctrineORMAdapter($queryBuilder);\n```\n\n### DoctrineODMMongoDBAdapter\n\nTo paginate [DoctrineODMMongoDB](http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/) query builders.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineODMMongoDBAdapter;\n\n$queryBuilder = $documentManager-\u003ecreateQueryBuilder('Model\\Article');\n$adapter = new DoctrineODMMongoDBAdapter($queryBuilder);\n```\n\n### DoctrineODMPhpcrAdapter\n\nTo paginate [Doctrine PHPCR-ODM](http://docs.doctrine-project.org/projects/doctrine-phpcr-odm/en/latest/) query builders.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineODMPhpcrAdapter;\n\n$queryBuilder = $documentManager-\u003ecreateQueryBuilder();\n$queryBuilder-\u003efrom('Model\\Article');\n$adapter = new DoctrineODMPhpcrAdapter($queryBuilder);\n```\n\n### DoctrineCollectionAdapter\n\nTo paginate a `Doctrine\\Common\\Collection\\Collections` interface\nyou can use the `DoctrineCollectionAdapter`. It proxies to the\ncount() and slice() methods on the Collections interface for\npagination. This makes sense if you are using Doctrine ORMs Extra\nLazy association features:\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineCollectionAdapter;\n\n$user = $em-\u003efind(\"Pagerfanta\\Tests\\Adapter\\DoctrineORM\\User\", 1);\n\n$adapter = new DoctrineCollectionAdapter($user-\u003egetGroups());\n```\n\n### DoctrineSelectableAdapter\n\nTo paginate a `Doctrine\\Common\\Collection\\Selectable` interface\nyou can use the `DoctrineSelectableAdapter`. It uses the matching()\nmethod on the Selectable interface for pagination. This is\nespecially usefull when using the Doctrine Criteria object to\nfilter a PersistentCollection:\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\DoctrineSelectableAdapter;\nuse Doctrine\\Common\\Collections\\Criteria;\n\n$user = $em-\u003efind(\"Pagerfanta\\Tests\\Adapter\\DoctrineORM\\User\", 1);\n$comments = $user-\u003egetComments();\n$criteria = Criteria::create()-\u003eandWhere(Criteria::expr()-\u003ein('id', array(1,2,3));\n\n$adapter = new DoctrineSelectableAdapter($comments, $criteria);\n```\n\nNote that you should never use this adapter with a\nPersistentCollection which is not set to use the EXTRA_LAZY fetch mode.\n\n*Be careful when using the `count()` method, currently Doctrine2\nneeds to fetch all the records to count the number of elements.*\n\n### ElasticaAdapter\n\nTo paginate an Elastica Query query:\n\n```php\n\u003c?php\n\nuse Elastica\\Index;\nuse Elastica\\Query;\nuse Elastica\\Query\\Term;\nuse Pagerfanta\\Adapter\\ElasticaAdapter;\n\n// Searchable can be any valid searchable Elastica object. For example a Type or Index\n$searchable = new Index($elasticaClient, 'index_name');\n// A Query can be any valid Elastica query (json, array, Query object)\n$query = new Query::create(new Term(array(\n    'name' =\u003e 'Fred'\n));\n\n$adapter = new ElasticaAdapter($searchable, $query);\n```\n\n*Be careful when paginating a huge set of documents. By default, offset + limit\ncan't exceed 10000. You can mitigate this by setting the `$maxResults`\nparameter when constructing the `ElasticaAdapter`. For more information, see:\n[#213](https://github.com/whiteoctober/Pagerfanta/pull/213#issue-87631892).*\n\n### PropelAdapter\n\nTo paginate a propel 1 query:\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\PropelAdapter;\n\n$adapter = new PropelAdapter($query);\n```\n\n### Propel2Adapter\n\nTo paginate a propel 2 query:\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\Propel2Adapter;\n\n$adapter = new Propel2Adapter($query);\n```\n\n### SolariumAdapter\n\nTo paginate a [solarium](https://github.com/basdenooijer/solarium) query:\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\SolariumAdapter;\n\n$query = $solarium-\u003ecreateSelect();\n$query-\u003esetQuery('search term');\n\n$adapter = new SolariumAdapter($solarium, $query);\n```\n\n### FixedAdapter\n\nBest used when you need to do a custom paging solution and\ndon't want to implement a full adapter for a one-off use case.\n\nIt returns always the same data no matter what page you query:\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\FixedAdapter;\n\n$nbResults = 5;\n$results = array(/* ... */);\n\n$adapter = new FixedAdapter($nbResults, $results);\n```\n\n### ConcatenationAdapter\n\nConcatenates the results of other adapter instances into a single adapter.\nIt keeps the order of sub adapters and the order of their results.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\Adapter\\ConcatenationAdapter;\n\n$superAdapter = new ConcatenationAdapter(array($adapter1, $adapter2 /* ... */));\n```\n\n## Views\n\nViews are to render pagerfantas, this way you can reuse your\npagerfantas' HTML in several projects, share them and use another\nones from another developer's.\n\nThe views implement the `Pagerfanta\\View\\ViewInterface` interface,\nwhich has two methods:\n\n```php\n\u003c?php\n\n/**\n * Renders a pagerfanta.\n *\n * The route generator is any callable to generate the routes receiving the page number\n * as first and unique argument.\n *\n * @param PagerfantaInterface $pagerfanta     A pagerfanta.\n * @param mixed               $routeGenerator A callable to generate the routes.\n * @param array               $options        An array of options (optional).\n */\nfunction render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array());\n\n/**\n * Returns the canonical name.\n *\n * @return string The canonical name.\n */\nfunction getName();\n```\n\nRouteGenerator example:\n\n```php\n\u003c?php\n\n$routeGenerator = function($page) {\n    return '/path?page='.$page;\n};\n```\nPagerfanta comes with five views:  The default one, three for\n[Twitter Bootstrap](https://github.com/twitter/bootstrap), one for\n[Semantic UI](https://github.com/Semantic-Org/Semantic-UI) and\na special optionable view.\n\n### DefaultView\n\nThis is the default view.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\View\\DefaultView;\n\n$view = new DefaultView();\n$options = array('proximity' =\u003e 3);\n$html = $view-\u003erender($pagerfanta, $routeGenerator, $options);\n```\n\nOptions (default):\n\n  * proximity (3)\n  * prev_message (Previous)\n  * next_message (Next)\n  * css_disabled_class (disabled)\n  * css_dots_class (dots)\n  * css_current_class (current)\n  * dots_text (...)\n  * container_template (\u003cnav\u003e%pages%\u003c/nav\u003e)\n  * page_template (\u003ca href=\"%href%\"\u003e%text%\u003c/a\u003e)\n  * span_template (\u003cspan class=\"%class%\"\u003e%text%\u003c/span\u003e)\n\nCSS:\n\n```css\n.pagerfanta {\n}\n\n.pagerfanta a,\n.pagerfanta span {\n    display: inline-block;\n    border: 1px solid blue;\n    color: blue;\n    margin-right: .2em;\n    padding: .25em .35em;\n}\n\n.pagerfanta a {\n    text-decoration: none;\n}\n\n.pagerfanta a:hover {\n    background: #ccf;\n}\n\n.pagerfanta .dots {\n    border-width: 0;\n}\n\n.pagerfanta .current {\n    background: #ccf;\n    font-weight: bold;\n}\n\n.pagerfanta .disabled {\n    border-color: #ccf;\n    color: #ccf;\n}\n\nCOLORS:\n\n.pagerfanta a,\n.pagerfanta span {\n    border-color: blue;\n    color: blue;\n}\n\n.pagerfanta a:hover {\n    background: #ccf;\n}\n\n.pagerfanta .current {\n    background: #ccf;\n}\n\n.pagerfanta .disabled {\n    border-color: #ccf;\n    color: #cf;\n}\n```\n\n### TwitterBootstrapView, TwitterBootstrap3View and TwitterBootstrap4View\n\nThese views generate paginators designed for use with\n[Twitter Bootstrap](https://github.com/twitter/bootstrap).\n\n`TwitterBootstrapView` is for Bootstrap 2; `TwitterBootstrap3View` is for Bootstrap 3; `TwitterBootstrap4View` is for Bootstrap 4 (alpha).\n\n```php\n\u003c?php\n\nuse Pagerfanta\\View\\TwitterBootstrapView;\n\n$view = new TwitterBootstrapView();\n$options = array('proximity' =\u003e 3);\n$html = $view-\u003erender($pagerfanta, $routeGenerator, $options);\n```\n\nOptions (default):\n\n  * proximity (3)\n  * prev_message (\u0026larr; Previous)\n  * prev_disabled_href ()\n  * next_message (Next \u0026rarr;)\n  * next_disabled_href ()\n  * dots_message (\u0026hellip;)\n  * dots_href ()\n  * css_container_class (pagination)\n  * css_prev_class (prev)\n  * css_next_class (next)\n  * css_disabled_class (disabled)\n  * css_dots_class (disabled)\n  * css_active_class (active)\n\n### SemanticUiView\n\nThis view generates a pagination for\n[Semantic UI](https://github.com/Semantic-Org/Semantic-UI).\n\n```php\n\u003c?php\n\nuse Pagerfanta\\View\\SemanticUiView;\n\n$view = new SemanticUiView();\n$options = array('proximity' =\u003e 3);\n$html = $view-\u003erender($pagerfanta, $routeGenerator, $options);\n```\n\nOptions (default):\n\n  * proximity (3)\n  * prev_message (\u0026larr; Previous)\n  * prev_disabled_href ()\n  * next_message (Next \u0026rarr;)\n  * next_disabled_href ()\n  * dots_message (\u0026hellip;)\n  * dots_href ()\n  * css_container_class (pagination)\n  * css_item_class (item)\n  * css_prev_class (prev)\n  * css_next_class (next)\n  * css_disabled_class (disabled)\n  * css_dots_class (disabled)\n  * css_active_class (active)\n\n### OptionableView\n\nThis view is to reuse options in different views.\n\n```php\n\u003c?php\n\nuse Pagerfanta\\DefaultView;\nuse Pagerfanta\\OptionableView;\n\n$defaultView = new DefaultView();\n\n// view and default options\n$myView1 = new OptionableView($defaultView, array('proximity' =\u003e 3));\n\n$myView2 = new OptionableView($defaultView, array('prev_message' =\u003e 'Anterior', 'next_message' =\u003e 'Siguiente'));\n\n// using in a normal way\n$pagerfantaHtml = $myView2-\u003erender($pagerfanta, $routeGenerator);\n\n// overwriting default options\n$pagerfantaHtml = $myView2-\u003erender($pagerfanta, $routeGenerator, array('next_message' =\u003e 'Siguiente!!'));\n```\n\n## Contributing\n\nWe welcome contributions to this project, including pull requests and issues (and discussions on existing issues).\n\nIf you'd like to contribute code but aren't sure what, the [issues list](https://github.com/whiteoctober/pagerfanta/issues) is a good place to start.\nIf you're a first-time code contributor, you may find Github's guide to [forking projects](https://guides.github.com/activities/forking/) helpful.\n\nAll contributors (whether contributing code, involved in issue discussions, or involved in any other way) must abide by our [code of conduct](code_of_conduct.md).\n\n## Acknowledgements\n\nPagerfanta is inspired by [Zend Paginator](https://github.com/zendframework/zf2).\n\nThanks also to Pablo Díez (pablodip@gmail.com) for most of the work on the first versions of Pagerfanta.\n\n## Licence\n\nPagerfanta is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhiteoctober%2FPagerfanta","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhiteoctober%2FPagerfanta","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhiteoctober%2FPagerfanta/lists"}