1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="learning.paginator.together">
4 <title>Putting it all Together</title>
7 You have seen how to create a Paginator object, how to render the items on the current page,
8 and how to render a navigation element to browse through your pages. In this section you
9 will see how Paginator fits in with the rest of your MVC application.
13 In the following examples we will ignore the best practice implementation of using a Service
14 Layer to keep the example simple and easier to understand. Once you get familiar with using
15 Service Layers, it should be easy to see how Paginator can fit in with the best practice
20 Lets start with the controller. The sample application is simple, and we'll just put
21 everything in the IndexController and the IndexAction. Again, this is for demonstration
22 purposes only. A real application should not use controllers in this manner.
25 <programlisting language="php"><![CDATA[
26 class IndexController extends Zend_Controller_Action
28 public function indexAction()
30 // Setup pagination control view script. See the pagation control tutorial page
31 // for more information about this view script.
32 Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');
34 // Fetch an already instantiated database connection from the registry
35 $db = Zend_Registry::get('db');
37 // Create a select object which fetches blog posts, sorted decending by date of creation
38 $select = $db->select()->from('posts')->sort('date_created DESC');
40 // Create a Paginator for the blog posts query
41 $paginator = Zend_Paginator::factory($select);
43 // Read the current page number from the request. Default to 1 if no explicit page number is provided.
44 $paginator->setCurrentPageNumber($this->_getParam('page', 1));
46 // Assign the Paginator object to the view
47 $this->view->paginator = $paginator;
53 The following view script is the index.phtml view script for the IndexController's
54 indexAction. The view script can be kept simple. We're assuming the use of the default
58 <programlisting language="php"><![CDATA[
61 // Render each the title of each post for the current page in a list-item
62 foreach ($this->paginator as $item) {
63 echo '<li>' . $item->title . '</li>';
67 <?php echo $this->paginator; ?>
71 Now navigate to your project's index and see Paginator in action. What we have discussed
72 in this tutorial is just the tip of the iceberg. The reference manual and
73 <acronym>API</acronym> documentation can tell you more about what you can do with
74 <classname>Zend_Paginator</classname>.