paginator-together.xml 2.9 KB

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