web development and photography
symfony
symfony and LDAP
May 1st
I’ve recently been looking at the essential (ie why isn’t it included in the core?) symfony plugin sfGuard. It offers are really nice, simple way of building a simple user, role, and permissions system into your symfony app.
The great thing is that with a little tweaking (documentation not great) you can add a custom LDAP lookup class, which overrides the standard built in user password lookup. Ok, you need to write your own class, or use a 3rd party one. But the basics of LDAP are built into php5, and it’s really not hard to do.
One problem I had was that sfGuard is designed to make it impossible to do an LDAP lookup and an internal check for a superadmin user. The reasons are a little esoteric, and have to do with mixing static and non-static contexts. Basically, the only option is to change one of the sfGuard library classes. Specifically sfGuardPlugin/lib/model/plugin/PluginsfGuardUser.php
Adding in this fixed things:
if ($callable = sfConfig::get('app_sf_guard_plugin_check_password_callable_both'))
{
$result = false;
$result = $this->checkPasswordByGuard($password);
if (!$result)
{
$result = call_user_func_array($callable, array($this->getUsername(), $password));
}
return $result;
}
which meant I had to use a special ‘check_password_callable_both’ option in the app.yml file. But at least this way it did checkPasswordByGuard() first, and then tried my added in LDAP class second.
Is there another way of doing this? I don’t know. Probably. But this seemed to work for me, and perhaps could be included in the sfGuard plugin by default. I guess I should get in touch with the developers to check…
symfony routes
Apr 28th
Routing rules in symfony are a way of getting simple url’s looking the way you want. So rather than ugly things that show everyone how your system works like:
matthewbull.net/index.php?id=3&detail=false&category=true&year=2008
you can have things like:
matthewbull.net/id/3/detail/false/category/true/year/2008
Even better than that, using symfony’s routing system allows you to completely decouple a url from your code. It allows this because routing is set through a YAML configuration file called routing.yml. So you can change your url in this file without having to worry about its impact on your code, and changing lots of tests for request parameters in your controllers. A simple rule like:
routename:
url: /*
param: { module: mymodule, action: myaction }
will magically pair each value to its name, no matter what order those pairs appear in in the url.
Suppose I didn’t want all the request parameters written out in the url above, just their values, or perhaps a slightly more user-friendly parameter name. Something like:
matthewbull.net/3/nodetail/category
This is no problem to do in routing.yml because you can assign alternative names, default values, or no name at all to the values given in the url:
routename:
url: /:id/nodetail/category
param: { module: mymodule, action: myaction, detail: false, category: true, year: 2008 }
which will mean the url given above will be interpreted as follows:
first value you put after the domain name will be assumed to be the id
‘nodetail’ in the url actually mean use the parameter detail with value false
category will be true, and year will be 2008 even though no year has been sent (ie 2008 is the default year)
Routing is a great way to get user-friendly urls, generate new urls for the same code, or refactor old urls without having to touch your code. They are perhaps a little tricky to get used to at first, but fairly quickly make sense. The main thing to realise is that rules work from top to bottom, and the first one to match is used. Quite often problems or unexpected results come from not ordering the rules correctly. Always put more general rules lower down!
symfony
Apr 23rd
I’ve recently been deploying some symfony (http://www.symfony-project.org/) applications at The University of Kent. I’m pleased with the results.

Symfony is an MVC framework, along the lines of Ruby on Rails, but based on PHP5. It is widely used, including yahoo bookmarks:
http://www.ysearchblog.com/archives/000376.html
and the beta of the new version of del.icio.us:
http://www.symfony-project.org/blog/2007/10/02/delicious-preview-built-with-symfony
Why?
So why did I choose symfony as a framework for web development at Kent, as opposed to the many others (for example cakePHP and codeIgniter)? There are lots of reasons, as I’ll show later. But the three most important are:
- it’s well documented
- there’s a good and helpful user community
- it’s very powerful
Huh?
Documentation? No, don’t need that. Well clearly that’s what a lot of frameworks seem to think. Although most frameworks – and certainly cake and codeigniter – have great communities, their documentation isn’t always great. For example, cake has a kind of ‘getting started’ document and then sort of leaves the rest up to you. Fine if you want to do basic stuff, but not so great if you want more.
Plugged in
The key thing with a good community, apart from helpful advice on forums, is the availability of plugins. Symfony has loads of those, as you can see here http://trac.symfony-project.com/wiki/SymfonyPlugins. Many are pretty esoteric, but a lot are really useful, such as a really nice user security plugin called sfGuard.
OOPs
As for being powerful, the thing you notice first off with symfony is that it’s only compatible with php5. Only? That seems odd. Brave almost. But surely they could’ve tweaked things a little to get php4 support? Once you take a look under the hood, you realise that symfony is totally object-oriented. Inheritance, mixins, overloading, public/private/protected. It’s all there, beautifully structured. What’s great about the use of OOP and php5 is that symfony uses autoloading, so a lot of classes (including plugins) are easily available without lots of includes all over the place.
Less is more
OK so symfony sounds complicated, and in many ways that’s true. The real power of the framework comes from its flexibility. There are dozens of configuration files which let you change many aspects of how your application works, from how the urls appear to fine details in a pre-generated backend system. This power creates complexity. But really, any good php developer ought to be able to cope with this kind of thing.
But the power of the system also lies in its potential to bypass many parts of symfony altogether. For example, you don’t have to use the built-in database abstraction layers. Just use anything you want instead, including (heaven help us!) raw SQL. You don’t have to use the built-in scriptaculous ajax methods (but why wouldn’t you).
Scriptaculous
Which brings me to another nice feature: ajax. Although not unique to symfony, it is nonetheless made spectacularly easy in symfony to do some really nice things with ajax. And I do mean the real asynchronous ajax stuff, not just the javascript eye-candy that has become known as ajax.
Saving time
One of the main reasons for using a framework is to give your code some structure, rather than the usual ordered chaos of a hacked-together website. That’s all very well, particularly for big projects. But a real plus for many developers is the ability to produce web applications quickly. symfony can help there too.
One feature that really helped me recently was the admin generator. Beautiful. I basically made a mini-phpMyAdmin interface in about 5 mins. Add to that the sfGuard plugin for authentication and authorization, and in about 10 mins you have a fully-featured admin system. And it actually looks good too.
There are also great time-savers for things that sane developers hate. Things like forms and input validation.
Summary
symfony is a great way to save time and develop good, maintainable code. It’s a powerful framework, and is surely set to grow, particularly now that yahoo have adopted it for a couple of applications. It’s not perfect, but offers a huge amount of power and flexibility, and speeds up application development significantly.
Bad fish & chips
Feb 6th
Web Publishing – Web Redevelopment
Feb 19th
Web Publishing – Web Redevelopment
I like the list of problems with Edinburgh’s website, but then it goes on to say that actually Edinburgh has a pretty good site compared to all the other universities… ouch.
web hosting
Aug 4th
I’ve been looking recently at getting new hosting sorted out. This site is currently hosted with easyspace, which I suppose is ok. But I just checked out the list of WordPress (yes I’ll be starting another blog there) recommended hosts and couldn’t believe the value for money offered by US companies. eg. Bluehost seemingly offer just about all the basic tools and languages you’d want to set up simple (or even more complex) sites for a ridiculously low price. Laughing Squid also offer a fair bit, but have the advantage of being just plain cool.
I guess there’s a *lot* more competition in the States, which really drives prices down.
Anyway, BlueHost offer Ruby on Rails support, which is becoming increasingly attractive – although I’m still not sure exactly what I’d develop with it.
I think it’s also possible to install Django (kind of a python-based equivalent of RoR), although the array of open source web development tools is beginning to get daunting bearing in mind the often steep learning curve and considerable investment of time and effort.
Sigh… there just aren’t enough hours in the day…
