Saturday, August 23, 2008

Getting started with Symfony

I have occasionally been asked (and accepted) to make websites for some of the activities I am involved with. As they all generally based on devoted volunteers and runs on a limited budget, has they usually also been hosted on budget hosting services. This means that I have usually been restricted to coding in PHP rather than Java or other high-end languages.

The last couple of times has the projects been about putting together sign-up systems for Solarisløbet (a crazy Danish scout race). And although I had a foundation to start out from, has the request always been so late, that it was a race to get something decent put together before the deadline - although I didn't have to start from scratch. There has always been some things missing - mainly because everything was build up from the bottom. Which - together with that I don't like PHP - made it a pretty unfulfilling experience. After having delivered the last system, I decided to find a way to be able to deliver quality on time. I figured, that what I needed was a good framework. In Java I would probably have used Struts or something similar. But I didn't know any frameworks for PHP.

After a bit of digging around, I finally decided on Symfony. I suppose it doesn't offer a lot more or different than many others. But it seems well-supported and mature. And hopefully I won't have to write as much stupid PHP code! So I set out to set-up a development environment on my workstation.

Setting up Symfony

Normally I would I have tried and mimic the production environment as closely a possibly. But as I am currently without a laptop or other suitable personal computer, so I didn't really want to install linux on the box - might be lack of confidence in my linux administration skills or just because people keeps sending MS Office documents and expect the receiver to be able to read them(!). After a recommendation from a friend, I decided to try out the WAMP Server. The process is based on the installation guides, but with a couple of twists for a cleaner install. Here is a quick breakdown of the process. I will give more details below:
  1. Install WAMP
  2. Install PEAR
  3. Update your %Path% - to be able to call pear and symfony directly on the command line
  4. Install Symfony
  5. Create Symfony project
  6. Update Apache virtual hosts
  7. Go wild! ;-)

1. Installing WAMP

Should be trivial - just follow the wizard. Only catch is that you need to enable mod_rewrite yourself the first time you start the server. Just click in the tray-icon and select Apache->Apache Modules->rewrite.

2. Installing PEAR

There is a bat-file in the php-directory. Execute it and hit enter each time it ask you something. Finally it makes a reg-file to update the Windows registry.
To be sure you have all the latest versions use the command:
> pear upgrade-all

3. Update your %Path%

To be able to run pear and symfony from the command-line, they need to be in your %PATH%.
I achieve this by simply changing the variable. I really prefer this simple approach to the messy one used in the Symfony on Wamp-guide, as it a lot cleaner and thus less error-prone.
To do it, simply open Control Panel -> System (or Properties on My Computer) -> Advanced -> Environment Variables. Find the Path variable and add C:\wamp\bin\php\php5.2.6; to the end of it. If it is not already there, just add it. I prefer to have it user variable, but that is mostly a matter of taste.
You have to restart command prompts for the change to take effect.

4. Installing Symfony

Open a command prompt and type in the two commands:
> pear channel-discover pear.symfony-project.com
> pear install symfony/symfony


5. Create Symfony project

The next two steps are as described in the Symfony book, but (of course) with a little twist. But that is first in the next step.

6. Update Apache virtual hosts

I am sort of a neat-freak. So rather than adding the virtual host directly in apache/conf/httpd.conf, I prefer to do it in apache/conf/extra/httpd-vhosts.conf.
To use this configuration file uncomment the line Include conf/extra/httpd-vhosts.conf in apache/conf/httpd.conf.
Then go to conf/extra/httpd-vhosts.conf and remove the dummy VirtualHost directives and add a VirtualHost directive for your project(s). It should look something like this:
<VirtualHost>
ServerName localhost
DocumentRoot "[path]"
DirectoryIndex index.php
Alias /sf "C:\wamp\bin\php\php5.2.6\data\symfony\web\sf"
<Directory>
AllowOverride All
Allow from All
</Directory>
<Directory>
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
The path to your project should be absolute.

7. Go wild! ;-)

You are now ready to start building your exciting Symfony-powered application!

This is of course only the start. Now I have to design the application and figure out how to express it within the Symfany framework. But that might be another blog-post... :-)

No comments: