Cake\Controller\Component\PaginatorComponent::paginate PHP Method

paginate() public method

### Configuring pagination When calling paginate() you can use the $settings parameter to pass in pagination settings. These settings are used to build the queries made and control other pagination settings. If your settings contain a key with the current table's alias. The data inside that key will be used. Otherwise the top level configuration will be used. $settings = [ 'limit' => 20, 'maxLimit' => 100 ]; $results = $paginator->paginate($table, $settings); The above settings will be used to paginate any Table. You can configure Table specific settings by keying the settings with the Table alias. $settings = [ 'Articles' => [ 'limit' => 20, 'maxLimit' => 100 ], 'Comments' => [ ... ] ]; $results = $paginator->paginate($table, $settings); This would allow you to have different pagination settings for Articles and Comments tables. ### Controlling sort fields By default CakePHP will automatically allow sorting on any column on the table object being paginated. Often times you will want to allow sorting on either associated columns or calculated fields. In these cases you will need to define a whitelist of all the columns you wish to allow sorting on. You can define the whitelist in the $settings parameter: $settings = [ 'Articles' => [ 'finder' => 'custom', 'sortWhitelist' => ['title', 'author_id', 'comment_count'], ] ]; Passing an empty array as whitelist disallows sorting altogether. ### Paginating with custom finders You can paginate with any find type defined on your table using the finder option. $settings = [ 'Articles' => [ 'finder' => 'popular' ] ]; $results = $paginator->paginate($table, $settings); Would paginate using the find('popular') method. You can also pass an already created instance of a query to this method: $query = $this->Articles->find('popular')->matching('Tags', function ($q) { return $q->where(['name' => 'CakePHP']) }); $results = $paginator->paginate($query); ### Scoping Request parameters By using request parameter scopes you can paginate multiple queries in the same controller action: $articles = $paginator->paginate($articlesQuery, ['scope' => 'articles']); $tags = $paginator->paginate($tagsQuery, ['scope' => 'tags']); Each of the above queries will use different query string parameter sets for pagination data. An example URL paginating both results would be: dashboard?articles[page]=1&tags[page]=2
public paginate ( Cake\Datasource\RepositoryInterface | Cake\Datasource\QueryInterface $object, array $settings = [] ) : Cake\Datasource\ResultSetInterface
$object Cake\Datasource\RepositoryInterface | Cake\Datasource\QueryInterface The table or query to paginate.
$settings array The settings/configuration used for pagination.
return Cake\Datasource\ResultSetInterface Query results
    public function paginate($object, array $settings = [])
    {
        if ($object instanceof QueryInterface) {
            $query = $object;
            $object = $query->repository();
        }
        $alias = $object->alias();
        $options = $this->mergeOptions($alias, $settings);
        $options = $this->validateSort($object, $options);
        $options = $this->checkLimit($options);
        $options += ['page' => 1, 'scope' => null];
        $options['page'] = (int) $options['page'] < 1 ? 1 : (int) $options['page'];
        list($finder, $options) = $this->_extractFinder($options);
        /* @var \Cake\Datasource\RepositoryInterface $object */
        if (empty($query)) {
            $query = $object->find($finder, $options);
        } else {
            $query->applyOptions($options);
        }
        $results = $query->all();
        $numResults = count($results);
        $count = $numResults ? $query->count() : 0;
        $defaults = $this->getDefaults($alias, $settings);
        unset($defaults[0]);
        $page = $options['page'];
        $limit = $options['limit'];
        $pageCount = (int) ceil($count / $limit);
        $requestedPage = $page;
        $page = max(min($page, $pageCount), 1);
        $request = $this->_registry->getController()->request;
        $order = (array) $options['order'];
        $sortDefault = $directionDefault = false;
        if (!empty($defaults['order']) && count($defaults['order']) == 1) {
            $sortDefault = key($defaults['order']);
            $directionDefault = current($defaults['order']);
        }
        $paging = ['finder' => $finder, 'page' => $page, 'current' => $numResults, 'count' => $count, 'perPage' => $limit, 'prevPage' => $page > 1, 'nextPage' => $count > $page * $limit, 'pageCount' => $pageCount, 'sort' => key($order), 'direction' => current($order), 'limit' => $defaults['limit'] != $limit ? $limit : null, 'sortDefault' => $sortDefault, 'directionDefault' => $directionDefault, 'scope' => $options['scope']];
        if (!isset($request['paging'])) {
            $request['paging'] = [];
        }
        $request['paging'] = [$alias => $paging] + (array) $request['paging'];
        if ($requestedPage > $page) {
            throw new NotFoundException();
        }
        return $results;
    }