Entries Tagged 'Programming' ↓

First Impressions of CakePHP

CakePHP logoAs I mentioned the other day I’m trying to work a new project in CakePHP which is the first time I’m working in the PHP framework. Actually I haven’t even really gotten into working with on the project yet - just working my way through some Cake tutorials. Even though it’s early in the process I thought I’d share some initial impressions.

Cake Forces Good Habits

One of the knocks against PHP in general is that it’s easy (and common) to mix your business logic, database access and display all together in one big mishmash. That’s certainly a valid complaint and I’ve done plenty of scripts that work that way but to be honest I don’t think that’s a terrible thing for a small project. Part of what motivated me to try Cake was the fact that Cake development follows the MVC (model-view-controller) pattern which separates the three parts into their own little area. I suppose it’s not entirely true that Cake forces this - you can certainly put all the PHP code you want into your template files - it would be crazy to try to use Cake without embracing MVC.

Cake Forces Domain Thinking

Thinking in the snow : Part 2
Creative Commons License photo credit: DetroitDerek
It’s easy as a programmer to start thinking in terms of code and database tables. “The blog table has a userid that’s a foreign key to the users table”. Since Cake handles the database actions for you, you need to think in domain terms and define those relationships in your Cake models - a blog belongsTo a user; a user hasMany blogs. I think this is good but requires a bit of a change in how you approach the task at hand.

Cake Makes Some Stuff Very Easy

If you want a website that allows you to add, edit and delete entries in a simple database table you can do it all in minutes with Cake. Maybe even quicker. Unfortunately while that can get development up and going quickly that’s not useful in the real world. First, few useful applications are simply working with one database table. Second that quickie app doesn’t include user authentication. Third that generated app likely doesn’t look much like something you’d want in production. You can do all that - work with more complex databases, add user authentication, change the look - but its not going to be done in minutes.

Some of the cake tutorials and materials will point out that “You just accomplished [something amazing] with just 3 lines of code!”. That’s great and you can do some very cool and complex things with Cake with very little code but keep in mind that the code is often not the easiest thing to come up with in the first place.

Cake Uses Tons of Conventions

Part of how Cake accomplishes so much with so little code is by using conventions. Your database table is named “users” - all lowercase and plural and contains a field named “id”. Your model is in a file named “user.php” - singular and contains a class named “User”. Your controller is in a file called “users_controller.php” and contains a class named “UsersController”. You’re not technically required to follow all those naming conventions but doing so is part of what makes things “just work” in Cake.

Tons of Anonymous Arrays

Lots of parameters are passed as arrays which lead to lots of code that looks like this:

$this->redirect(array(’action’ => ‘index’), null, true);

As a gift to your favorite Cake developer you can find a key board that has a single key that will generate the word “array”. It’s a minor thing and I suspect something you quickly get used to.

Is It Worth It?

Innes Birthday cake
Creative Commons License photo credit: geishaboy500
I still have a long way to go to being productive with CakePHP but so far I think it’ll be worth the effort. Partially because of the promise that once you get proficient, development will get very fast. Partially because Cake handles the grunt work very well letting you focus on the business logic and domain work. Partially because it’s just fun to be working with some new and fairly advanced technology.

In general I learn best by just digging in and doing stuff and I think that’s the next step for me with Cake. I’ll update in the future with how it goes from here.

Things that suck about open source

I spend at least 90% of my day immersed in open source software. My web browser is FireFox. My server runs Linux, Apache, MySQL and PHP. I’ve recently been working in the excellent PHP framework CakePHP. This blog is powered by the amazing open source WordPress. There’s no doubt in my mind that these projects are all best of breed and I wouldn’t be able to do most of what I do without them.

There is, however, another side of open source that drives me crazy. That is the vast number of half-finished abandoned projects that litter the web.

The other day I was looking for a good PHP class for handling user registration/login. It’s obviously something that nearly every website deals with. While there are a number of projects out there I wasn’t able to find one that’s well developed and does what I need. That’s ok. The problem is the large number of partially working solutions that are abandoned. On many open source repositories it’s difficult to impossible to determine the exact state of a project. Most importantly is it still being actively developed.

When picking an open source project - especially one that will be incorporated into a larger project as opposed to one that will be used stand alone - it’s vital to find a project that’s current, actively undergoing development and that has a community of users that will support it.

But here’s the good thing about open source. Most of the time if is project is abandoned it’s possible for someone else to come along and take it over. Or at the very least it should be possible to use an existing project as the basis for a new open source project that you can maintain, actively develop and build a community around.