Using PHP - Resque with Silex and the Symfony2 Classloader

11 May 2012

Resque is a popular Redis-backed Ruby library for creating and processing background jobs, this is all well and good if you're building applications in Ruby but fortunately for those, like me, who prefer PHP there is also a PHP port - PHP-Resque. This post will describe how to use php-resque in conjunction with the Silex micro framework to queue jobs, initially this was not immediately obvious to me as php-resque is not namespaced so wouldn't work using the default autoloading configuration but fortunately the class naming seems to follow the PEAR convention where the path to the class is defined by the classname (i.e. Resque_Event would be found in Resque/Event.php). The method described here should work for any similar libraries.

One point to note, there is a Symfony2 bundle that could be easily integrated with Silex which aims to integrate php-resque but it's a lot more complicated than I require and in most cases is just not necessary.

The key to using php-resque is to use the registerPrefix method of the symfony classloader (included with Silex) which uses PEAR naming conventions load libraries.

To configure php-resque first clone a copy of the library from github into the vendors folder (or wherever you like to store your external libraries):

Now we have the library in place where ever you configure your application, in my case /src/bootstrap.php which is included in the main application file point the Synfony class loader to the place for classes beginning with the prefix Resque, all the relevant php-resque files are in the lib subdirectory: