A note on Zend Pagination

Having gone round in so many circles I finally found out that when useing Zend Pagination and the current route you are using has more than one dynamic variable i.e

$router->addRoute(
                'listingswithpage',
                new Zend_Controller_Router_Route(
                        '/listing/:type/:page',
                        array('controller' => 'listing',
                            'action' => 'index')
                )
        );

Your pagination view helper gets a bit thrown if you go by the documentation. The first thing you need to do is find a way to specify the first page initially. I went with specifying my first page in my route definition so it looked something like this

$router->addRoute(
                'listingswithpage',
                new Zend_Controller_Router_Route(
                        '/listing/:type/:page',
                        array('controller' => 'listing',
                            'action' => 'index',
                            'page' => 1)
                )
        );

And that worked. Failing that you have to figure out a way to pass the complete URL path to you pagination view which might be something like “controls.phtml” or “pagination.phtml”. and constructing the url properly there.

I will perhaps post a more detailed description of what this means but leave a comment if you are impatient and I will try and respond when I get a moment.

Mod_Rewrite and Zend Framework 1.10.x and Ubuntu

So all the tutorials on configuring your development server to run Zend Framework successfully say that you need to enable mod_rewrite in apache2 so that .htaccess and Zend_Router* works well.

What they all say is to make sure it’s loaded and enabled. I will raise my hand and say it wasn’t that easy for me to find because Ubuntu lays out Apache2 a little differently from say Fedora. First of all in the /etc/apache2 folder there are a few folders to take note of.

sites-enabled and mods-enabled actually tell you what has been enabled or not and not an uncommented list in some httpd.conf file. Basically the folders in there have symbolic links that come from sites-available and mods-available. This perhaps is a better idea as you can add new mods and not have to recompile apache or whatever.

I digress, to enable mod_rewrite in apache2 in Ubuntu 10.04.1 you need to create a symbolic link from mods-available to mods-enabled to do this you can either use the command:

sudo ln -s /etc/mods-available/rewrite.load /etc/mods-enabled/rewrite.load

or if you prefer GUIs you have to run nautilus as root so you can try

sudo nautilus

and navigate to “/etc/mods-available”, right-click on rewrite.load and select “make link” and the cut the new file created called “link to rewrite.load” paste it in mods-enabled and rename it to “rewrite.load ”

Now all you need to do is restart your web server and you’re all set to reroute anyhow you want.

sudo /etc/init.d/apache2 restart

A note on CAPTCHA decorators and Zend_Form_…

So if you have been struggling with setting up decorators with Zend_Form_Element_Captcha, here’s a note.

I used to get an additional textbox with the hash returned by Zend creating the captcha image. And if you were stumped as to how to remove them from your display try the following.

First of all your form element please note that I extracted this from my extension of the Zend_Form class

$captcha = new Zend_Form_Element_Captcha('captcha',
 array('label' => 'Type in the text you see in the image',
 'captcha' => array('captcha' => 'Image',
 'wordLen' => 6,
 'timeout' => 300,
 'height' => 60,
 'width' => 250,
 'font' => APPLICATION_PATH . '/../public/assets/fonts/arial.ttf',
 'fontSize' => 30,
 'imgDir' => APPLICATION_PATH . '/../public/assets/captcha/',
 'imgUrl' => 'http://' . $_SERVER['HTTP_HOST'] . '/assets/captcha/',
 ))
 );// there are certainly more options here but not necessary for my purposes.
//You can try altering the noise levels in the captcha image such as how many
// dots and lines should be created wiuth the image
// google noise levels in Zend_Form_Element_Captcha

This generates my captcha and I can add it to my form by going with

$this->addElements(array($email,$captcha,$submit));

Bear in mind that $name and $email and $submit are also new instances of their respective Zend_Form elements. In other words don’t include them if you’re copying and pasting.

Now after adding your elements you have to define a separate decorator for the Captcha because it’s obviously different from other form elements so here goes:

$captcha->setDecorators(array(
 'Captcha',
 'Errors',
 array('Label', array('separator' => '<br />', 'requiredPrefix' => '* ')),
 array('HtmlTag', array('tag' => 'p', 'class' => 'form-element'))
 )
 );

Hope it’s helpful