Introduction to the Yii Framework

Final product image
What You'll Be Creating

Why Yii?

Writing "Hello World" in PHP is quite easy but building an actual web application becomes complicated quickly. As soon as you want to manage security, write DRY code, or leverage MVC design techniques, PHP seems lacking. In the past I've worked with Ruby on Rails but found that there's still a lot that's hard with it - especially when it comes to hosting and deployment in an economic and flexible manner. 

This is where PHP frameworks come in.

There are a number of good PHP frameworks, Symfony, Cake, Zend, CodeIgniter, Laravel and my favorite, YiiYii is a free, open-source framework for PHP5 that promotes clean, DRY design, and supports rapid development. I've found that Yii offers the breadth of everything I expect from a framework in a high performance package with few of the limitations other PHP frameworks inflict.

Upstart media site Vice is written in Yii as is the emerging developer's alternative to WordPress, CraftCMS (follow @reifman for my upcoming Intro to CraftCMS).

Yii's stands out for its performance, which is critical when you adopt a framework:

The Yii framework also offers a breadth of capabilities that allow me to quickly and easily write optimized web applications. And importantly, Yii also offers excellent documentation and a supportive community.

Yii is mature, time-tested and stable. Created by developer Qiang Xue, Release 1.x has been around since 2008. Release 2.0 is expected momentarily, they're just finishing up the documentation and final testing.


For a high performance framework, Yii's breadth of capabilities and feature set is quite extraordinary, and it's one of the reasons I was attracted to the framework. You can see and try some examples of Yii code in action at the Yii Playground.

Here are some highlights:

  • Model-View-Controller architecture. Just like Ruby on Rails, now you can leverage MVC in your PHP apps.
  • Database Access Objects (DAO), Active Record, and programmatic Database migrations simplify the challenges of building database-powered web applications.
  • Form input, validation, and Ajax support is built-in. Yii makes forms pretty easy.
  • Built-in authentication and powerful user management extensions make launching new web applications easy.
  • Yii's built-in code generation tool, Gii, speeds your app's development scaffolding in MVC fashion
  • Console Yii. You can run Yii from the command line or as a daemon. With this, it's possible to build high performance background tasks in PHP.
  • Theming options such as the Bootstrap extension makes building great looking responsive apps much simpler
  • Layered caching support Yii makes it easy to implement the kinds of caching that make sense to your application
  • Security. Yii greatly minimizes the typical risk factors of running services with PHP and MySQL
  • Integration with other frameworks. It's easy to use Zend or PEAR features in Yii
  • Extensions. Yii's community offers a variety of free, open source plugins and widgets
  • Internationalization. Yii supports I18N and makes it easy to provide localized versions of your app.
  • Error handling, logging and testing - yes, Yii delivers.

The Yii community also purchased an unlimited license to the beautiful web-based rich text editor, Redactor - so you can use it any of your own Yii apps. This is great for rich formatted input or CMS features.

The Yii documentation and community are also great features. There are a few great ebooks to help you get started.

My Experience with Yii

Yii's been a key part of my increased productivity over the past two years. Yii's made it easy for me to build a number of web applications:

  • Simplify Email filters, manages and sends digests for my email, simplifying the hassles of modern day communication and enable powerful new features such as email quiet hours. It integrates with the PHP IMAP library, the Mailgun API as well as the Pushover API for notifications. 
  • Geogram is a concept project to offer email groups to neighborhoods and places. Geogram integrates with Zillow neighborhood data, Google Maps, Google Places and Mailgun.
  • Simple Monitor monitors my WordPress sites and the health of my servers. When something goes wrong, it sends an iOS notification to my phone.

The code for Simplify Email and Simple Monitor is available for purchase if you'd like to review some deeper Yii code. I've also written a handful of Yii tutorials

Some of my favorite Yii extensions include Yii User, Yii Bootstrap, Redactor and Yii Hybridauth.

What's New in Release 2.0?

Yii 2.0 is poised for release (follow the development roadmap). The Yii2 beta notes best summarize the improvements from Yii 1.x. 

In part because of how advanced Yii 1.x was, there isn't a single amazing feature to Yii2, but there are a broad set of substantial improvements which will make life increasingly easier for Yii developers. 

Here are a few highlights:

  • Support for PSR-4 class autoloading, simpler namespaces, faster loading and improved usability for developers
  • Performance and security improvements
  • RESTful API framework integration to make building APIs easier
  • Codeception testing integration
  • Database and active record improvements including batched queries, support for sub-queries and inverse relations
  • Improved URL handling and processing
  • Translations of core messages available in 26 languages
  • Documentation generator

Getting Started with Yii

Getting started with Yii is pretty easy because of its outstanding documentation. Here are a few things to try:

I'm tremendously excited about Yii 2.0 and the future of Yii as a PHP framework. It's leveled up my efficiency as a developer and consultant. I hope you find Yii as interesting and useful as I have.

If you have any questions or corrections, please post them in the comments. If you'd like to keep up on my future Tuts+ tutorials and other series, please follow @reifman or visit my author page.



Related Articles