Just makes life as a developer a lot easier!
Some people are lame, for example this site:
And since I don't want to give the site any traffic what so ever you won't get a link here. Sorry!
Zend Pagination is a topic that is intimidating at first but more like a cute puppy that will play with you for hours in the end. The reason I make this analogy is because I have done a lot of digging through Zend's documentation and various blogs to get things to work correctly, only after trial and effort did things work out and work out quite well and effortlessly when it all came together.
First lets ask us what we are paginating. For example we will be paginating a set of rows in a database. The database can be abstracted away and you may insert your own database table where necessary. There are two things I am going to assume you have set up already. The model for your data as well as the controller that will take the model data and pass it to the view. First lets start with a higher level entity and set up the appropriate routes for our pagination so we can pass in a clean url such as "http://www/domain.com/browse/3". As you can see the segment after "browse" lets us know what "page" of data we are on. In index.php add the following code:
What this code does is set up two paths to the controller. One is the default path which will handle a person that heads to the url without specifying a page segment and the second handles the page segment and turns that into a variable called "page". Please read Zend's documentation here for more information regarding routes.
Lets take a look at the model. The model in this case extends Zend_DB_Table_Abstract, however in earlier tests I have found that this class does not have the necessary functions and member data to satisfy our paginator. I ran into examples where I would have to build select statements for the entire table and pass that into the paginator with a custom row count. After doing some extensive digging I came upon the following class: Zend_Paginator_Adapter_DbTableSelect. This class will do exactly what we are looking for. It contains the number of rows in the table (necessary for pagination) as well as selecting the right rows using a page size and page number. Here is a condensed version of my model:
What I have shown you is a way to mould the class to something that can be returned and used in our controller action. The class takes as its main parameter a select statement (Zend_Db_Table_Select). I have modified the select statement to include additional fields and ordering criteria. As I mentioned previously the data is an abstraction for us at this point, you can insert your own table parameters and sorting criteria as needed. To learn more about the Zend_Paginator_Adapter_DbTableSelect click here. If you also have other questions about models in Zend Framework I suggest you read the quick start guide located on Zend's website or Rob Allen's guide. Both are good reads. Now that we have established what our model will be doing we will now have to take a look at our controller. Here is the excerpt from my controller that really matters:
This is our browse action. Most of the code is self explanatory but lets step through it. Remember when we set up a router for browse and used "browse/:page". What we really did was set up a way for ZF to interpret the url and take any segment after "browse" and turn it into an internal variable at our disposal. When we make a call to $this->_getParam() we are looking for our "page" variable. If the variable does not exist we use a default of 1. We use the default just in case the user comes to the page without a "page" segment. We initialize our model by creating an instance of it. From there we create an instance of Zend_Paginator, which can be initialized with many different things please look here for all the options. You can build a paginator with an array if you wish but then you must manually calculate the total rows for the table, the page offset, the page size, etc... A lot of work for something that should be a simple task.
Now that we have an instance of our paginator we can set some variables such as the cache for the paginator(I turned it off by passing a false flag to the function), the page size, and the current page. Once all those options are set you can then pass the paginator to the view and then begin displaying data. Below is a watered down version of my view:
Remember that our paginator is responsible for selecting the subset of data that represents the page from our database table. We iterate through the pagination object as it contains the rows that it selected and we output the rows one by one. The most important part of this view is the code at the end which is the paginator display code itself. To my dismay Zend Framework does not have its own view template for pagination. You must create a view script in the application/views/scripts folder and then supply that file name as the third parameter to paginationControl(). In my view I use $this->paginationControl($this->paginator, 'Sliding', 'paginationControls.phtml');. The first parameter is an instance of the pagination object itself. The second parameter is the type of pagination controls you wish to display. I choose the Yahoo style of pagination known as the "Sliding" style. For more elaboration on styles of pagination please read here (The section you are looking for is titled "Rendering pages with view scripts"). The third parameter is the file we placed in our pagination displaying code in:
Since the *.phtml file is an extension of a view we have all our helpers available at our disposal. This makes it elegant to create links such as "Next", "Previous", etc... that link to our pages. There are many different internal variables that are in Zend_Paginator that designate the aforementioned items, it would do you well to read the docs to figure them all out as they come in handy based on how custom you want your pagination controls to be.
I was doing some experiments with Zend Framework and I found that using Captcha is a neat way to keep spammers off of your site as they must manually validate the image that represents the Captcha word. At first I started off with Zend_From_Element_Captcha and decided to run with the generic "Image" implementation. This was awesome at first but had is consequences in the end. What happens when you have over a thousand requests hit the form and it has to generate the Captcha elements? The directory doesn't get too big but there is additional CPU overhead to generate all these images and then store them in a directory. Enter ReCaptcha! ReCaptcha is a service from Google that helps you integrate captcha sequences into your form. Additionally Zend Framework offers you such a easy way to integrate the service into your forms. For example take the following code:
That's it! Simple, sleek and elegant. All you have to do is remember to use your public and private keys from your ReCaptcha account. When validating all you have to call is the traditional "$form->isValid($_POST)" function. Shout out to Ryan @ http://www.rmauger.co.uk/ for sharing the knowledge to make this happen.
