Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /tutorials/paginator.together.html

Size:12516
Storage flags:no_autoload,compress/gzip (30%)

Putting it all Together — Zend Framework 2 2.4.2 documentation

Putting it all TogetherΒΆ

You have seen how to create a Paginator object, how to render the items on the current page, and how to render a navigation element to browse through your pages. In this section you will see how Paginator fits in with the rest of your MVC application.

In the following examples we will ignore the best practice implementation of using a Service Layer to keep the example simple and easier to understand. Once you get familiar with using Service Layers, it should be easy to see how Paginator can fit in with the best practice approach.

Lets start with the controller. The sample application is simple, and we’ll just put everything in the IndexController and the IndexAction. Again, this is for demonstration purposes only. A real application should not use controllers in this manner.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class IndexController extends Zend\Controller\Action
{
    public function indexAction()
    {
        // Setup pagination control view script. See the pagination control tutorial page
        // for more information about this view script.
        Zend\View\Helper\PaginationControl::setDefaultViewPartial('controls.phtml');

        // Fetch an already instantiated database connection from the registry
        $db = Zend\Registry\Registry::get('db');

        // Create a select object which fetches blog posts, sorted descending by date of creation
        $select = $db->select()->from('posts')->order('date_created DESC');

        // Create a Paginator for the blog posts query
        $paginator = Zend\Paginator\Paginator::factory($select);

        // Read the current page number from the request. Default to 1 if no explicit page number is provided.
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));

        // Assign the Paginator object to the view
        $this->view->paginator = $paginator;
    }
}

The following view script is the index.phtml view script for the IndexController’s indexAction. The view script can be kept simple. We’re assuming the use of the default ScrollingStyle.

1
2
3
4
5
6
7
8
9
<ul>
<?php
// Render each the title of each post for the current page in a list-item
foreach ($this->paginator as $item) {
    echo '<li>' . $item["title"] . '</li>';
}
?>
</ul>
<?php echo $this->paginator; ?>

Now navigate to your project’s index and see Paginator in action. What we have discussed in this tutorial is just the tip of the iceberg. The reference manual and API documentation can tell you more about what you can do with Zend_Paginator.

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Putting it all Together on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net