Every now and then I like to re-develop this site as a way of improving my skills or to use some new techniques and technologies. Its a great way of having complete freedom over what you produce and lets you experiment a bit more than you might be able to when developing a site for someone else. This time I decided to use Yii to run my site.
What is Yii?
Yii is a PHP framework – a library of functions and classes that help you get your web site up and running faster than if you had to write everything yourself. There are a few established PHP frameworks out there such as Zend, CodeIgniter and Symfony, and perhaps Yii isn’t quite as well known as these yet but it appears to be gaining popularity. Yii’s developers named it from the answer to commonly asked questions when choosing a framework: Is it fast? Is it secure? Is it professional? Is it right for my next project? ‘Yes It Is!’
Like many other frameworks, Yii provides the basis of your web site or application by taking care of the CRUD functions (create, read, update and delete database records). This leaves you free to focus on your application’s unique requirements.
The reasons for using any framework instead of writing the entire site or application in house are generally the same:
- Save time. As mentioned, if a framework provides you with a skeleton of your site then that should reduce the development time
- Re-use of code in future projects, saving more time and preventing having to re-invent the wheel
- Quality code. Frameworks should provide comprehensively tested and secure code, giving you a good platform to build from
- Separation of code. Many frameworks, including Yii, implement the MVC pattern (model, view, controller). This keeps the code used to display the pages separate from the database back-end and the logic of your application. This promotes cleaner code and encourages code re-use
- A large number of extensions to the framework that provide added functionality without too much development work
Yii is open source and uses the MVC and ActiveRecord Object Oriented Programming patterns to deliver what its developers claim is faster loading speeds than other frameworks.
The right choice?
If I was selecting a solution to build this site with based purely on what would be the quickest and easiest approach then I wouldn’t have chosen Yii. I probably wouldn’t even have chosen any framework and would have gone for a Content Management System like Drupal instead. This site is pretty small: a few static pages, a blog and a secure area for authenticated users. Yii can be used for projects of any size but it has been developed with large scale applications in mind.
However my objective was to use this version of the site as an opportunity to improve my Yii and Object Oriented PHP knowledge. Yii appealed to me from research I had done for other projects. In particular, I felt Yii had:
- Excellent CRUD support
- A good automatic code generator using a GUI web interface called Gii that would produce the database interaction and application ‘rules’
- Fast performance with its lazy loading approach to classes so it only loads what it needs
- Good integration with the likes of jQuery
- And it looked pretty fun to use compared with some other frameworks. Not many people seemed to have many strong criticisms of it. Unsurprisingly I found that people tended to have a bias towards whatever framework they were most familiar with
If you were developing an application that would be around for a long time and go through many different versions that maybe Zend would be a better choice. As the framework from the core contributors to PHP, you can be pretty sure Zend is here to stay but from my brief use of it, I found it a bit OTT for what I needed to do and that it had a steeper learning curve than Yii.
Disadvantages of Yii
Many of the disadvantages I found of using Yii could probably be levelled at any framework:
- There is a learning curve to climb and how steep and difficult that is will depend on your previous programming experience and whether you have used other frameworks
- Despite the promise that frameworks will reduce development time, it can seem at times that you are being slowed down by a framework. The simpler the project, the more true this is likely to be. After all, most people with PHP experience could knock up a simple site that reads and writes to a database without having to learn a framework
- You are forced into doing things in a certain way by the MVC approach. And if something appears impossible then you are probably doing to the wrong way!
- There is a degree of bloat with the code. If you are used to writing sites yourself, from HTML and CSS to PHP classes, then you may find yourself wondering why Yii needs all these other files. The same could be argue about some CMS but Yii’s lazy loading should ensure it only loads what it needs
But I did like it!
What I did find with Yii is that generally I could find the answers to my questions in the forums already, without having to post new questions. There were some extensions I could take advantage of fairly easily. There were not too many books on the subject but hopefully this will change. The best I found was Agile Web Application Development with Yii1.1 and PHP5Ã‚Â.
When choosing a framework, at some point you have to bit the bullet and pick one. As far as Yii is concerned, I haven’t seen anything that would make me discourage its use but I have seen plenty to recommend it.