At work I was tasked with building an internal application that is designed to closely mimic the popular Ffffound.com. Our designers have a lot to share but we are lacking a single robust channel for them to do so. Currenly they use email, but that method is disruptive and very difficult to have robust discussions for the whole community to see.
So with a basic set of objectives in hand I started to orgainze my thoughts on how I would build this. I decided to dig deep into CakePHP and start with nothing and see how far CakePHP could take me. I’ve learned a lot in the last week, and I plan on sharing much of it here on this blog. Ive found that the cake community is big on sharing, and the numerous blogs out there actually make up for a sometimes lacking manual and API.
I chose not to use codeignighter because it is so lightweight. If I was going to really learn MVC for PHP I wanted to learn it on the most robust framework available, that way any future work with a framework would either be status-quo or a step down in complexity.
So I started looking thru the Manual, the API and the Bakery for direction. To be frank, there wasn’t a whole lot of ‘getting started’ help. The manual has two application examples, one of them being the classic simple blog (using scaffolding). Although the examples were step-by-step I really found that they dont quite explain why things are happening, instead they sort of just tell you what to do to get things working. I could learn this way but it would take me forever to try to reverse engineer the process. I needed something else, another approach.
I set out looking on the interwebs for some help and I came across this article on Snook.ca where Jonathan explains his initial thoughts. He explains how he installed cakePHP and it literally told him what to do to get started. I thought this might be a good way to learn, actually get into the app and begin throwing around my own code, making it do what I wanted, poking here and there and seeing for myself what happens. Sure enough, when you install cake it starts telling you what you should do and the steps to do it.
Let’s say you want to show all your posts at the url /posts/. You plug that into your browser and cake will display a page telling you how to build out the code to make that page start handling code. It was not long before I was putting together the parts of this application, writing CakePHP code and exploring the manual for what I needed, instead of searching for explanations.
Some Discoveries
As with any large scale open source project, you will do yourself a huge favor to read through the source and see how the experts think. I am always amazed to see how people optimize functions, both for efficiency and for readability. One method I came across was the use of PHP compact() to set a named array as an option parameter.
To briefly explain compact(), it creates an array by combining variables supplied to it. The interesting thing is that the variables are named, not supplied. This means your arguments to compact are strings, not variables. As I’m using it i find it really cleans up the source code for functions that require nested arrays with named variables. This approach to array building has some hidden power, since now you could create the array by splitting strings and without even knowing the variables that will go into building it.