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.

Having multiple instances of Zend Navigation in a single View

so I have been playing around with the Zend Framework rather extensively over the past few weeks and it has been a very rewarding experience.

I am posting this here because it was a bit difficult finding a work around for the issue I describe below.

Zend Navigation had been giving me a lot of problems especially because I could not get the Navigation object passed to my view object to render two different navigation configs. The top menu which remains the same for all pages and is rendered in my layout and other sub menus rendered in my individual view pages.

Now getting to it:

in my bootstrap I initialise my navigation with something like this.

$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
 $miscNavConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/miscnavigation.xml', 'nav');
 $navigation = new Zend_Navigation($config);
 $miscNavigation = new Zend_Navigation($miscNavConfig);
 Zend_Registry::set(Zend_Navigation, $navigation);
 Zend_Registry::set('MiscNavigation', $miscNavigation);
 $view->navigation($navigation);

Now  lines 2, 4, and 6 are the new lines I have added for additional nagivation. I beleive you should be able to add new configurations as you move along if you need to.

The next bit was retrieving the sub navigation in my controller:

$miscNavigation = Zend_Registry::get('MiscNavigation');
$this->view->miscnavigation = $miscNavigation;

You can do this in your view but because I route several of my views through this controller I want to have to do it only once.

And finally in my view:

$options = array('ulClass' => 'submenu');
echo($this->navigation()->menu()->renderMenu($this->miscnavigation, $options));

Bear in mind that I use the renderMenu method instead of passing $this->miscnavigation into the menu method. If you do that you will overwrite any other instances of menu you have on the page with the miscnavigation object.