Sunday, December 6, 2009

Yii Framework: choosing the best web application framework

After experimenting with a few different programming languages and even more frameworks, I've settled upon a PHP framework called the Yii Framework. This post describes how I arrived at Yii and what other options I considered before making my decision.

Several years ago I wrote a functioning, but ugly and difficult to modify, web application. The code that's in production right now works well, as I spent a lot of time making sure it was solid and had good cross-browser support. However, there are several new features I'd like to add and some issues I'd like to address before it takes off and makes me a millionaire (we all can dream, no?). But the convoluted and frustrating mess of spaghetti code in my original implementation has stalled these ambitions.

For the past few years I have been too busy with work and other projects, and this one has regretfully stagnated. From time to time I felt the itch and would set out to find the perfect language + framework in which to build my application. Following is a rough chronology of my various attempts to rewrite and extend my application using "the best" web application framework.

Attempt #1: PHP + Zend Framework beta

Having experience with PHP, and comforted by the fact that Zend Framework is built by the people who built PHP, my initial foray with ZF seemed like an good idea despite its pre-1.0 status.

The main problem I ran into with ZF was my own ignorance.

  1. I wanted my hand held, but Zend Framework was more of a toolkit than an MVC environment.
  2. This was my first experience with a sophisticated web application framework. Conceptually I thought I understood the Model-View-Controller pattern, but I'd never used one before and didn't fully grasp its nuances or the best practices.
  3. Despite having a few years of experience with Java, I'd only programmed strictly procedural PHP up to that point. In retrospect this really wasn't a major hurdle, but coupled with the other issues it was a contributing factor.

The quick start guide was not nearly as good as it is today, and getting everything set up was frustrating. I thought I was missing the how-to guide that would explain how to get everything the way I wanted and send me on my way. I didn't really understand what a framework was or what ZF offered.

Attempt #2: PHP + CakePHP

After being frustrated by the learning curve and the lack of guidance with Zend Framework I stumbled upon CakePHP and got excited once again. It offered a structured, this-is-how-you-do-it, approach to building web applications.

The lack of documentation in some key areas is what eventually drove me away from CakePHP. I spent many hours trying to get some forms to behave exactly how I wanted them to, and after several days without much to show for it coupled with a few ugly "hacks" in place to get the behavior I wanted, I didn't feel like things were going the way I wanted. Things slowly fizzled out as I became overwhelmed by other projects, and the rewrite halted.

Attempt #3: Ruby on Rails

While considering job prospects after my imminent graduation from college, I briefly got caught up in the Ruby on Rails phenomenon. I ordered a book, built a simple application, and was thoroughly blown away. Everything just clicked. The hype was justified and my only regret was not having taken a look at RoR sooner.

After a while, however, I ran into some issues that nagged me too much to continue on. Ruby on Rails was at first refreshing in its "one way -- the right way" approach to designing web applications. After the agnosticism of Zend Framework I was initially receptive to Rails.

But the considerable, backwards-incompatible differences between the dated version of Rails used in the books I purchased, and the more recent versions, caused several headaches. I spent hours trying to figure out how to do what I considered to be very simple things for common use cases. When I did find the answers I was usually surprised by their simplicity, but the hunt for them was the challenge, and the fact that I was learning Ruby at the same time made progress inch ahead at a snail's pace.

I think I could have been successful with Ruby on Rails if I was more patient at the time, but it required more of an investment than I was willing to give at the time. I did enjoy learning the basics of Ruby, but I was disappointed to discover that it was not my silver bullet.

Other concerns were limited web hosting support, more complicated deployment steps, and various claims of limited scalability. At the time, the first was (and may still be?) a legitimate concern. The other two may not have been issues so much as excuses I deferred to as I shifted my time to other pursuits and stopped working on the rewrite.


I realized at this point that programming frameworks have an up-front investment in time that cannot be ignored. One cannot simply port their code to a framework without learning and accepting the various quirks of the framework (and language). As I only had a small amount of time available to invest and I was looking for immediate results, I shelved the project yet again.


Attempt #4: Python + Django

Quite a bit of time passed since the previous attempt. After hearing about python for years I finally decided to see what it was all about one evening, and found myself glued to the very well-written introductory Python tutorial. After working in Java professionally for quite some time, learning python was really... fun. It seemed to have the interesting superpowers of those languages you learn in college but never use professionally, but with a large user-base, excellent documentation, few technical limitations, wide support and an inexplicable "wow, that's cool" factor.

I took a look at the small selection of viable Python web application frameworks and after much fretting over cutting edge vs. conservative options, settled upon Django. It was really quite easy to get started with but I eventually (inevitably) realized once again that I was starting down a path where I needed to be serious and willing to invest a lot of time if I was going to be successful. I didn't have the time to invest, so things stalled yet again.

Attempt #5: PHP + Yii Framework

After gaining professional experience in a variety of frameworks in a couple different languages, and feeling more disciplined and confident that I can master a chosen language/framework, I have selected the Yii PHP Framework. I make this choice after much research and contemplation, not because I believe Yii is the best framework out there, but because my initial experimentation has been quite enjoyable and when I attempted to solve the issues I faced in previous attempts I found that Yii offered simple, documented solutions.

To be honest, once I settled on PHP as the language this was really a toss-up between Yii and Zend Framework. I investigated ZF's progress since my initial attempt and found that it has really become a lot more solid and full-featured than it was a few years ago. If I had more experience with Python I'd probably be seriously considering that option, but, alas, I'm eager to get something finished quickly and I know I have the skills in PHP to be up and running quickly.

The main negative for Yii is the fact that it is relatively new, and is still technically in beta. Having kept an eye on it for quite some time now, seeing it implemented successfully by several companies, and knowing how brilliant the lead programmer is all give me some comfort that it is a legitimate contender. The other factor that pushed me to Yii is that I can still use Zend Framework components inside my Yii application if I choose.

Why not Code Igniter, newer versions of CakePHP, Symfony, or any of the other myriad of PHP frameworks out there? I can't claim to have fully vetted all options because I simply don't have the time. I took a look at documentation, examples, the size and vibrancy of communities, and even many flame wars between the fanatics of each, and felt that Yii was the best fit for my purposes.


P.S. Please do not take offense to my personal experiences if I had problems with your framework of choice. I know there are experts who can bend each of these frameworks to their will to accomplish almost any task, and I'm absolutely not saying that any of these frameworks is the best choice, or not a good choice, for anybody. These are simply my experiences with the version, documentation and time constraints I faced at the time I tried each of them out. I would enjoy going back and trying out some of these frameworks a second time to see how they have improved since I gave them a spin.