Craig Marvelley

Software and such

Using a PHP Framework With Windows Azure

[This is a blog post related to the PHP on Azure competition.]

It’s been a while since my last PHP on Azure contest post, mostly due to commitments @work that have reduced my free time considerably. There’s not long left ’til the competition closes though (I have a week), so I need to get my skates on!

Casting my mind back to my last post, I was able to finally deploy to the local development fabric. In the interim I’ve set up an Azure account and successfully deployed a simple script to the cloud, so everything’s fine on that front. It’s time to start developing my application.

Since my time is limited my application will be quite straightforward – a CMS to manage media files. This should allow me to use all the features of Azure (queuing, table storage, blobs) I’m interested in.

Bittersweet Symfony

The first step in building the application is to decide on  framework to speed up development. The main reason I got into the contest in the first place was because I wanted to have more experience with Zend Framework – while I’d used its components in isolation before I’d never built an application with it. However in the months since I entered the competition I’ve started using Symfony 2 heavily. That, and the realisation that ZF2 is around the corner, have made me think that ZF isn’t the best choice for this contest, so I decided to use Symfony 2 instead.

That was easier said than done. I dropped the Symfony framework into my Azure WebRole project, and put together a ‘hello world’ script, only to immediately run into this issue – apparently Azure has issues when building projects when paths to files or directories exceed 260 characters. Of course, in a framework of Symfony’s size, with many nested directories, this limit is unrealistic.The solutions recommended on Jim’s blog include changing the project paths to be shorter (impossible in this case) and setting a temporary environment variable which moves the location at which Azure builds projects, thus reducing the prefix of any paths. As Jim says, this isn’t a guaranteed fix and indeed, it didn’t work for me.

So including Symfony within the WebRole didn’t seem to be possible. I experimented with including in within my PHP include path, in the hope that it would save some characters, but that didn’t work at all, Azure didn’t even pick it up. I need to look around the net to see how Azure manages include paths, since it’s something I use often in PHP. How do PEAR modules work, I wonder? Can you manage PEAR within Azure as you would with a local PHP installation, or do you have to place the components within each WebRole? Topics for future posts, maybe.

Anyway, it didn’t look like Symfony was going to work any time soon, so I needed an alternative, and since Ben Waine had successfully deployed using Zend Framework I thought briefly of using that after all. Then, I came across Silex.

Along came a Silex

Silex is a micro-framework based on Symfony 2 components, inspired by Sinatra, the Ruby on Rails framework which presents an alternative to typical MVC web applications. It’s succinct and straightforward, and, crucially, is deployed as a PHAR archive. This means I only need to include a single file in the root of my project to be able to use the library – which means no long paths that Azure can’t handle.

I downloaded the Silex archive from its website and dropped it into the root of my project, then copied the ‘hello world’ example provided within the Silex documentation into my index.php file:

require_once __DIR__.'/silex.phar'; 

$app = new Silex\Application(); 

$app->get('/hello/{name}', function($name) {
    return "Hello $name";
}); 

$app->run();

Then I deployed my project to dev fabric, and navigated to /hello/Craig. Of course, this didn’t work immediately – nothing ever does for me – because I didn’t have a web.config file set up to send all requests through the front controller, index.php. I got an IIS 404 page rather than the ‘Hello Craig’ I was expecting. So I used IIS’s Apache config importer to convert the sample .htaccess file provided by Silex into a web.config file, then copied the relevant rewrite rules into my WebRole’s config file. Rebuilding the application resulted in a working application. Good to go!

Here’s what I needed to add to my web.config file in case it’s of any use to anyone:

Compiling Silex on Windows

Because Silex is pretty new I’m expecting to find issues here and there, plus when working on Windows I’m accustomed to having to make little changes here and there to get PHP code that’s been written on *nix to run properly – so I wanted to build the Silex PHAR from source so I could make changes if necessary and properly understand how everything fits together.

This is quite straightfoward. First, I checked out Silex from its github page:

git clone https://github.com/fabpot/Silex.git silex

I merged in a pending fix which addresses compilation issues on Windows – there’s probably a fancy way to do this in git but I just applied the fix manually.

Then I needed to get Silex’s dependencies:

git submodule init
git submodule update --recursive

Silex provides a PHP script, named ‘compile’, which puts the archive together. I had to make some changes to my php.ini file before I could run it successfully:

phar.readonly = Off
phar.require_hash = Off

Then creating the archive was as simple as running:

php compile