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

Image Manipulation with Zend

I had been on the prowl for an MVC approach to image manipulation. Especially a method that did it the Zend way. In the forums and documentation for the framework the developers didn’t see a real need to implement a “Zend_Image” class in the libraries as this was already available through the PHP GD and Imagick hooks.

Some of us however did and there are a few tutorials on how to incorporate the PHPThumb library into Zend. I have used the PHPThumb Library on the mokocharlie.com project and found it I however stumbled on a much simpler way of doing this that feels more native to the framework Zend_Image is that implementation hidden somewhere on the code.google.com hosting servers.

$image = new Zend_Image( APPLICATION_PATH.'/../public/media/images/large/'.$filename,
   new Zend_Image_Driver_Gd());
   $transform = new Zend_Image_Transform($image);
   if($image->getWidth() > $image->getHeight()){
       $transform->fitToWidth(295)
       ->save(APPLICATION_PATH.'/../public/media/images/medium/'.$filename);
       $transform->fitToWidth(147)
       ->save(APPLICATION_PATH.'/../public/media/images/small/'.$filename);
       $transform->center()->middle()->crop(74, 74)
       ->save(APPLICATION_PATH.'/../public/media/images/tiny/'.$filename);
   }else{
       $transform->fitToHeight(300)
       ->save(APPLICATION_PATH.'/../public/media/images/medium/'.$filename);
       $transform->fitToWidth(147)
       ->save(APPLICATION_PATH.'/../public/media/images/small/'.$filename);
       $transform->center()->middle()->crop(74, 74)
       ->save(APPLICATION_PATH.'/../public/media/images/tiny/'.$filename);
   }

The above is my implementation of it as I create three versions of an uploaded image.