CouchDB and PHP Web Development

Support this website by purchasing prints of my photographs! Check them out here.

I recently got my hands on the book CouchDB and PHP Web Development: Beginner's Guide, written by Tim Juravich and published by Packt. This book aims to provide the reader with enough information to build their own website powered by PHP and using the CouchDB, a NoSQL database.

When I first picked up this book, I was expecting a boring, text-book approach to code examples for PHP talking with CouchDB. Boy was I wrong. What I found was a book that has you build a complete working application. And by complete, I mean you'll add the Twitter Bootstrap framework and it will be sexy. You'll do things the hard way, and refactor with an easier method. You'll be building a PHP framework with controllers and views. You'll be integrating a third party library for communicating with CouchDB which follows a similar simplistic approach as the framework. You'll also be keeping track of everything in GIT and committing to GitHub. These are all very important things for any application developer to know, and this book will get you into some good habits.

The book starts off with an introduction to general NoSQL concepts (schema-less, fast), as well as the history of database systems. It then gets into the history of CouchDB itself. Nothing in there was too surprising, however the author did put a larger emphasis on the lack of ACID compliance than I have seen elsewhere (which means data isn't 100% guaranteed). This is a great thing to point out; it prevents CouchDB from being a fit for many projects (such as financial) however perfect for other projects (high traffic logging applications) and the risk and reward gradient for every application in-between.

The author also guides the reader through the complete OS X setup process for getting Apache started, configuring it to work with PHP, getting the homebrew package manager working and subsequently CouchDB installed, running, and persisting between restarts. This means the beginning programmer can take on building this application without any prior knowledge of PHP development. I thought this was a great touch, it is probably the biggest thing holding back people from programming.

Overall, this book does a great job explaining REST and the HTTP verbs, how those translate into their CRUD counterparts, how JSON is a great data language to be sent over the wire. Early on we use command line cURL for interacting with the CouchDB server, a slightly tedius method. I absolutely LOVE that the author had us run through cURL requests in the command line BEFORE sending us to the web-based GUI administration app, Futon. This puts a huge emphasis on exactly what kind of data is sent over the wire, something most MySQL developers, for example, will never know.

In the third chapter of the book, the author has us dive into securing our CouchDB installation. This is an important topic that could have been otherwise omitted, since it isn't necessary to get an application up and running. By default, CouchDB is insecure, and many a developer may not ever realize this until something bad happens. By calling it out early on, the code doesn't need a large refactor later on to bolt-on authentication.

The book has us building a simple PHP framework, named Bones by the author. Of course, we could have skipped building this framework and simply used raw PHP scripts (e.g. having .php extensions in your address bar). This would have been a bit easier for the reader to handle, and wouldn't have required the configuration of mod_rewrite in apache.conf. However, by using this framework, the reader is more likely to explore different frameworks down the road, instead of taking a simpler route. There is a small risk though that the user won't fully understand how Apache maps URI requests to the filesystem. Overall though, by having pretty URLs the completed application looks and feels like more of a solid accomplishment, bringing confidence to the reader.

Another thing worth mentioning about the Bones framework is that it makes heavy use of PHPs Anonymous Functions. I personally love this method, as it feels a lot like the Node.js Express framework. PHP didn't get support for Anonymous Functions until version 5.3, which did come out a few years ago and is largely the standard. As a PHP beginner, by learning recent features of the languages, they may be more likely to grok the language than users learning from antiquated tutorials. The framework is currently hosted on GitHub, but it appears largely dead, not having been updated in a year.

The author also guides the reader step-by-step through creating a GitHub account, something that can be a little scary to beginners. I used to teach a PHP meetup regularly, and getting an account setup was one of the hardest things to do (it would require an entire session). Creating an SSH key, and doing other configurations on the command line can be daunting, but it really is necessary. So many great projects are hosted on GitHub and the sooner a developer can get in the club the better. After each big milestone, the author has us committing our changes and pushing to GitHub. This is an excellent habit to get into and the author puts a good emphasis on it.

When it gets time to have PHP begin communicating with CouchDB, the author has us do so using the cURL library built in to PHP. While cURL is incredibly powerful, it is very difficult for beginners to use. It is also overly verbose. It is generally not appreciated by the PHP crowd and there are many libraries (such as Requests for PHP) built to encapsulate the difficulty with a clean, Object Oriented interface. Luckily for us, the author was using cURL to show us one way of doing things (e.g. the hard way) before switching to a better solution. He soon has us install a library specifically meant for working with CouchDB called Sag, which follows the same beginner-friendly, simple methodology as the Bones framework. He also guides us through refactoring our existing code.

The Bones framework soon gets an upgrade as we add a PHP autoloader to load Sag (and later the rest of our classes). The author really does a great job of “sneaking” in a lot good practices like this. Some other nice things that sneak their way in are using Gravatar for displaying users avatars, and using simple jQuery calls for making DELETE requests and fading out newly deleted items. There is also an emphasis on code refactoring and removing redundant code, and the reason we do so is explained nicely.

Of course, the application we've bene building so far is not the prettiest (Times New Roman is everyones enemy after all). Thankfully, we soon add support for the Twitter Bootstrap framework. This allows us to quickly get a good looking website up with minimal HTML boilerplate code. Throughout the book we display code in the browser using normal text, then spruce it up with markup to follow Bootstrap methodologies.

A lot of PHP developers start by using MySQL as their Database System. To help make the transition to a NoSQL database system easier for those developers, the author often compares a schema-less CouchDB document to an equivalent MySQL tables and relationships. The two approaches to building database management systems are quite different, so this is a helpful comparison.

At the end of the book, we take out application and deploy it to some cloud services. The author has us go through the process of setting up a PHP Fog hosting account, as well as a Cloudant CouchDB hosting account, and of course getting the two to communicate. Both of these services offer free accounts, so this doesn't hold back the user. The account setup pages have screenshots for us to follow along and we are told how to get the PHP application to connect to the remote CouchDB instance. We also take our local CouchDB and have it replicate to the remote server, a nice feature which didn't have to make the cut for a beginners book.

One thing that I didn't quite understand is why we used the CouchDB users database instead of rolling our own. It could be that I'm not as familiar with CouchDB as I should be, but it seems like an application specific way would be better than a database specific method. CouchDB uses salted SHA1 password encryption, which we've seen a lot of negative press around over the past year or so. By rolling our own encryption we could have used a more secure encryption such as bcrypt.

Another thing I wasn't a fan of was enabling hidden files in Finder. Personally, I would have had the users use the `ls` command in terminal, and mention that there are hidden files that aren't normally shown in Finder. For editing the config files, we could have used the `mate` command provided with Textmate (the text editor the author suggests). By using the command line more it might have made the reader a little more comfortable with it. Thankfully the author does provide a command to disable hidden files once we're done.

Overall, CouchDB and PHP Web Development Beginner's Guide is an amazing book for any beginner looking to get into building PHP applications to persist data using CouchDB. There are many good practices sprinkled into the pages within. Anyone running a computer with OS X can follow along, even if they have no prior experience with programming or databases. The verbose code explanations do a great job explaining what the author is having us build. If you are interested in either of these technologies I highly recommend picking up this book.

Thomas Hunter II Avatar

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript and is an O'Reilly published author.