home / blog / rad-dev-a-case-study
# Rapidly developing an application with Lithium: a case study

This article aims to introspect an app that has been developed using the most rad PHP framework, Lithium. As of this writing, Lithium is still in early development status and not recommended for production. However, due to the simplicity of the app combined with the framework being as efficient and easy to work with as it is, I was able to quickly develop, launch and even [begin collaborating](http://rad-dev.org/lithium_anologue) with fellow developers on this experimental, open-source app dubbed [ Anologue](http://anologue.com).

## think before you write

Before I started coding anything, it was important for me to think about the purpose of the app. I needed clearly defined goals in order to focus on what was absolutely necessary to accomplish them. Simplicity was important in all aspects to get up and running quickly. Then, I could focus on refactoring, experimentation, etc.

My initial goal: enable any number of people to view a web page that would serve as a sort of chat-room. Generate a link, share it with whomever you'd like to participate in the dialogue, type your name and text to add to the conversation.

## the database

Part of the process in thinking about my app was thinking about the data I would be collecting and delivering, and, the database I would use. Having had the pleasure of working with [ CouchDB ](http://couchdb.apache.org) while working on [ Pastium ](http://pastium.org), though my experience was very limited, I knew it was appropriate for what I was setting out to do.

Because I didn't plan to implement users, categorization, or any other relational elements, the document-oriented approach of CouchDB was exactly what I was looking for. Also, because documents in a collection (or table) need not share a schema, I was able to easily add, remove and modify the model data I was storing and retrieving in the model itself as I was developing. Basically, all I had to do in CouchDB was create the collection and never look at it again. The rest was all PHP. It was difficult to continue programming at times, as I could hardly see through the tears of joy.

## [the model](http://rad-dev.org/lithium_anologue/source/models/Anologue.php)

I decided the model data would consist of a basic array of messages, each containing the message itself and a few other pieces of information that might prove useful (adding to it as I went along.) Functionally, I needed to be able to create a new document, read a document according to a specific id, and update the document by appending a new message to the array of messages contained. Because CouchDB works across HTTP, I needed to urlencode my messages before being saved, and therefore urldecode my messages upon retrieval. That's basically it. _Editor's note: most of this is now handled automatically by Lithium's official CouchDB adapter._

## [the controller](http://rad-dev.org/lithium_anologue/source/controllers/AnologueController.php)

Just like the model, the controller needs three simple actions. Create a new model, read a model, and add a message to an existing one. All pretty straight forward. 

One of my favorite aspects of lithium so far is the ability to dump your data in JSON form directly from the controller. It's such a simple idea, and the code equally simple:
{{{$this->render(array('json' => $data));}}}
Also, the use of static objects makes working with models in controller logic way more... logical. You have to [see to believe](http://rad-dev.org/lithium_anologue/source/controllers/AnologueController.php#highlight). 

If you look at the code you may notice the `$status` variable I throw around. This is because I decided to conform to the [JSend specification](http://labs.omniti.com/trac/jsend) when using AJAX to receive JSON data. The spec is quite simple and, though I don't know if there is widespread adoption (yet?) it just makes sense to have predictable, consistently formatted responses. If you haven't explored this spec, I highly suggest you give it a look.

## [the views](http://rad-dev.org/lithium_anologue/source/views/anologue/view.html.php)

Now, I say views, but really, this app only needs one 'view' (the other view in this app is really just a page — I wanted to stick to one controller.) 

The View-view serves to display the input form to submit a message, cycle through and display any existing messages in the desired html format. After the fact, I added some help for n00bs and a shortcut to the id of the anologue to quickly share the link. 

## [the javascript](http://rad-dev.org/lithium_anologue/source/webroot/js/anologue.js)

The rest isn't Lithium specific, but may provide some insight into how some of the other pieces come together and provide for inspiration in developing your apps. The overall approach was actually snagged from what we put together for the rad-dev site, particularly the cli-nav. We basically construct an object on page load that attaches some events to some various elements and provides a few app-specific methods we'll utilize throughout the process. In this case: the submission of a new message, polling for new messages, and processing of those new messages to generate and insert the html.

## bonus round

It was simple enough to add Markdown support using Showdown — which I feel adds value for technical users looking to share code, or some of the more nerdical bloggers wanting to style text. Also, an email field was added to utilize gravatars, adding a bit of personality when desired. Finally, utilizing the HTML5 audio element (and bit of JS to roughly account for variances in browser support) — if a new message is processed that includes the name you currently have entered - a sound effect is triggered.

## now what?

Embarrassingly, as small as this app is I haven't written tests for it, yet. Also, some ideas for the future include utilizing some third party providers to share images and other media (imgur, drop.io), moving to MongoDB and experimenting with a sort of "users-currently-viewing" concept. If you aren't sure where to start with an app of your own in Lithium, the door is open to [contribute to Anologue](http://rad-dev.org/lithium_anologue).

See ya in the lab!

~ [ Jon](http://twitter.com/pointlessjon) ~