paginator-together.xml 3.0 KB

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