Pink Ribbon Breakfast

I don’t normally plug work that I do in my daily grind, but this one is for super-great cause. Instead of our usual advertising selves, we’re using our powers for good and completely upgrading the National Breast Cancer Foundation‘s Pink Ribbon Breakfast site. Their goal is to have 0 deaths by 2030, which is a pretty nice goal all-round.

We built the site using Zend Framework, CSS3, and a healthy dose of jQuery-inspired Javascript.

Jump over to the site and check out our relatively-quiet first phase launch while we get ready for the donation-crazy second phase. And be sure to visit the donate link if you know someone with breasts.

Using a single controller in Zend Framework

Are you using Zend? Do you have only a core controller and don’t need extraneous URL parameters? Try this.

Most of the time, I am using Zend Framework to its (mostly) full capacity. Models, Controllers, View Helpers – the whole box-and-dice. However, there has been several occasions where I’ve only needed a single controller serving up mostly static pages, or pages leveraging an external service.

The biggest problem that arises in this case when using the default Zend Router, is that action names are treated as controller names. I don’t know about you, but I personally hate seeing http://example.com/index/my-action when what I actually want to see is http://example.com/my-action.

Here’s a nice way to completely tidy up all your actions, serving them from a single controller. (I use a Bootstrap file to initialise my application):

This basically says that my first parameter is always going to be an action name, and everything else is to be parsed as normal. I set request defaults in the array. In this case, the default controller is “index”. This allows everything to be passed straight through to my IndexController.

Addtionally, if you are using modules and want to retain routing for these also (for example, a one-controller site with an admin module), you can add this route rule to your system:

This says that anything starting with “admin” needs to be routed to the admin module, and the rest of the URL is to be parsed as normal.

That’s it! This will allow you to route all requests to a single controller for the front-end, and still maintain a complete module for the administration on the back-end.

Hopefully this cleared up some things for others, as I found the Zend Framework documentation to be quite dismal.

Using memcache and Zend_Cache

Probably one of the more useful things you can do with memcache is drop it into Zend Framework. Here’s how I did that.

Following on from the previous post, here is an example of how you can set up memcache to serve as a cache for your Zend project.

I like to use the method style of bootstrapping my application, so here’s the function that builds the cache for me.

Originally, I was getting the “Memcache::addServer function expects at most 6 parameters, 8 given” warning. To force Zend to play nice with memcache (and send the right parameter count), you need to make sure you have the compatibility option set to true in the backend options array.

Here’s an example on how to use:

Hope this helps in getting started with Zend_Cache (even on a technology as old as memcache).

Enabling memcache under MAMP

I know the tech is a bit old, but sometimes you need to make do with the tools you’ve been given. Here’s how I enabled memcache under MAMP.

First, grab the precompiled binary file (memcache.so) from here. There were no problems using this on Snow Leopard (for me, at least).

Second, copy the file to your MAMP extensions directory. You should be able to find it here: /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-[date].

Next, you’ll need to enable the extension in MAMP. Switch it off, then open up php5.ini by going to File > Edit Template > PHP5 php.ini

Find the extensions section (about line 540 or so) and add this line to the list of extensions:

Once that’s done, save php.ini and reboot MAMP. You should be able to see an entry for memcache when opening your phpinfo page.

Reclaiming your favicons when using MAMP

I use MAMP Pro to manage my local virtual hosts. It’s about a hundred times more convenient than Apple’s “native” Apache/PHP install (unfortunately a sad truth).

One annoying facet, though, is that MAMP will force its favicon when you’re using a MAMP-managed virtual host. Here’s how to get rid of this “feature” and reclaim your presumably awesome favicon.

First, make sure MAMP is not running Apache. Then, go to File > Edit Template > Apache httpd.conf

Once you’ve got the configuration file open, look for the line (roughly 625 for me):

Alias /favicon.ico "/Applications/MAMP/bin/favicon.ico"

and comment it out with a hash (#). Save your configuration, then reboot MAMP’s Apache server. You might have to flush your browser’s cache before it recognises your own favicons.

This took way too long to fix, so I hope this helped someone else.

Simple Remote Requests with Titanium Appcelerator

Titanium Appcelerator is a pretty nifty tool. Here’s a small data manager object that looked after my external requests.

I was tinkering with Appcelerator recently, and came upon the need to build a data manager to shoot off requests in a bit of a “fire-and-forget” sort of way. I created this global object I could channel requests into. My particular server always replied with JSON, so you can tweak this for whatever data your remote server will return.

Using this object in the global scope, you can just type:

Appcelerator has sped up development in key areas tremendously, but at the cost of some flexibility and code bloat. It’s worth keeping an eye on, as each release is leaps and bounds ahead of its predecessor.

Accessing local sites on VirtualBox

One of the most frustrating things about setting up VirtualBox for the first time is setting up a local host. It isn’t immediately obvious, and finding documentation was difficult. When you know how to get it running, it’s easy.

Here’s how:

1. Set up your VirtualBox image as normal.

2. On the networking tab, make sure you have the primary network adapter set to NAT

3. Boot your VirtualBox image and open your ‘hosts’ file using Notepad. This can be found at C:\Windows\System32\drivers\etc

4. Add your host entry as it is found on your main machine. In this example, I’m using the my site’s development copy ‘timneill.local’. See below.

5. Save the hosts file and enter your address into the browser. You should see your locally served site.

This was tested using Mac OS X and VirtualBox running WinXP. There is no need to flush the DNS cache for Windows after WinME.

If there are any problems, leave a comment or send me an email.

iRiver Story and Mac OS X

I acquired an iRiver Story recently, and couldn’t figure out how to mount the disk on my Mac OS X machine. The device didn’t automount, and it didn’t show up in the attached devices list.

A bit of scrounging led me to the solution:

  1. First, make sure the iRiver is plugged in.
  2. Power the device on, and wait for it to boot.
  3. When the prompt appears for charge or USB connection, choose USB connection.

The key thing to note here (and it was the problem I ran into) is that the prompt appears for about three seconds, which is barely enough time to figure out what you want to do with it. If you miss the prompt, simply unplug and re-plug the device.

© iRiver

As for document management… you can just drag and drop your files into the directories shown in Finder. You can (if you like) grab something like Adobe’s Digital Editions, but at the end of the day you don’t really need something so bloated to manage a handful of digital books.

Watching your Language

Inspired somewhat by Dave Ray’s post “Say Something Nice About Every Language You’ve Used“, I’ve decided to do the same.

PHP
Fairly easy to learn, and with an improving OO model, Web development will only get better. Syntax is pretty straight forward and it’s easy to get something working relatively quickly.

Visual Basic
Very simple syntax, and a good introductory language for learning Windows programming.

C/C++
I like C (and C++) because you can do with it what you will. There’s a lot of flexibility and room to move for whatever problem you have.

Java
It has a similar syntax to C, but automatic garbage collection makes me weep tears of joy.

Javascript
One of my favourite aspects of web development is putting on my Javascript hat and adding very slick functionality to my site. Simple syntax and a lot of power for overriding default objects.

Objective-C
Very quick to learn the basic syntax, and it forces a separation of code into conceptual pieces that I agree with.

That exercise is actually enlightening. I think focussing on the strengths of a particular language allows you to leverage it for maximum effectiveness. I’m fairly sure I’ve picked up more languages than this in the past, but not with enough experience for any sort of nice opinion. Perhaps next time I’ll just bitch and moan about all the languages I’ve used.

Return JSON data with Zend Framework

Getting Zend to return JSON data can be tricky at times. The JSON helper likes to send the ‘official’ JSON header (application/json), and most browsers are treating that as a download link, resulting in no data to the client and an unfortunate download box.

To bypass this default behaviour, you’ll need to tell the controller how to serve your JSON data:

The key part of this stub is the exit; line. It prevents the view renderer from sending anything, keeping your JSON data intact. Note: json_encode and json_decode only works from PHP version 5.2.1

To output all of your view’s variables in this way, replace $some_var; with:

Hopefully this saved someone a headache or two when dealing with Zend’s puzzling JSON implementation.