Introduction to the Yii Framework
I have three decent-size Web sites to do in 2009, so I thought I might try using a PHP framework for the first time, instead of coding everything from scratch. I’ve used Ruby on Rails for Web development before, so I’m comfortable with frameworks and the MVC architecture, but I wanted to educate myself on PHP frameworks. After researching a handful of frameworks, and after an unsatisfying attempt to use Zend Framework, I finally settled on, and really came to appreciate the Yii Framework. The Yii Framework is still quite new, and the documentation isn’t expansive, but it works so well that it’s still quite easy to use. In this first of several posts on the Yii Framework, I just discuss setting up and testing Yii.The first thing you need to use the Yii Framework is have PHP and a Web server installed, of course. (Well, that used to be true. As of June 9th, you can now try Yii using a virtual appliance. From Yii’s Web site, you can download a pre-built server: Linux OS, PHP, Web server, and PostgreSQL database. The appliance runs using VMware’s Server utility, which is free.) But if you’re reading this, I’m going to assume you have access to a PHP-enabled server. Note that the Yii Framework does require PHP 5.1 or above. Fortunately, the framework will test your setup for you! Start by downloading the latest stable version of the Yii Framework. At the time of this writing, that’s 1.0.6. The file you download will be named something like yii-version.release.ext and is only around 2MB. Expand this to create a folder of stuff:
- CHANGELOG, LICENSE, README, and UPGRADE text documents
- demos folder
- framework folder
- requirements folder
You should read the README and LICENSE docs, of course, but the folders are most important here. The demos folder contains four Web applications written using Yii. They’re great for seeing working code as you’re trying to write your own. The framework folder is what’s required by any Web site using Yii. The requirements folder is something simple and brilliant…
I also assume that you already know what the Web root directory is on your computer or server: this is the folder where your URL points to. In other words, when you go to http://localhost or http://www.example.com in your Web browser, it grabs documents out of the Web root folder. Going with Yii’s conventions, I’ll call this WebRoot. Create a new folder in WebRoot called yii, and copy the framework and requirements folders there. Then go to yourURL/yii/requirements in your Web browser (for example, http://localhost/yii/requirements). You should see a report as to whether or not your setup meets the minimum requirements.
Assuming your setup passed all the requirements, you’re good to go on. Note that you don’t necessarily need every extension: you just really need the Yii Framework requirements, PDO, and the PDO extension for the database you’ll be using. (If you’re not familiar with it, PDO is a database abstraction layer, making your Web sites database-agnostic.)
In my next post, I’ll show you how to use the command-line Yii tools to create your first Web application. It’s pretty sweet stuff and is the closest thing to Ruby on Rails that I’ve seen (which I consider to be a very good thing). Subsequent posts will explain MVC and highlight specific tips and tricks I discovered while developing my Yii-based sites. EDIT: Here’s the next in the series on Yii. Here’s the start of the series on MVC.


I look forward to you next posts on yii. I just moved the new site I’m developing over to yii and am enjoying so far. I’ve used CodeIgniter for a similar project in the past.
Comment by Jonathan — June 23, 2009 @ 8:54 am
Hello Jonathon. Sorry for the delayed reply and the lack of posts. It’s been an unfortunate and unpredictable week. I hope to start posting again later this week, including detailed discussions of Yii. I’ve taken quite a few notes on the framework and will write little tutorials on doing basic, and not-so-basic, tasks. Glad you like Yii, too!
Comment by Larry — June 27, 2009 @ 2:25 pm
I have a concern that while a framework can help with a big part of a project, it may ultimately become a hindrance because of limitations it imposes on developers.
Does Yii allow using other technologies for producing views? I use PHPTAL and like it very much and would like to use it with a framework if I decide to use one.
Comment by Roman — June 29, 2009 @ 7:25 pm
Really interesting one, i much appreciate. Thanks for sharing the information.
Comment by testking — July 3, 2009 @ 10:46 pm
I was quite relieved to see you’re going to be posting again soon. I just found, and absolutely love, your PHP Quickstart book. Your writing, and the columnar format of the book (with full code and not snippets, mind you, which are hard for newbies) are perfect.
I’ll be joining you on the yii train…I just checked out their site and it looks pretty darn hot.
It’s an awesome time to be learning web development, with all the advances going on, and I’m seriously glad for your input. I had already found you online some months back after being blown away by another of your books…so this raving isn’t without some historical enthusiasm!
Hope things are working out…
Comment by Patrickaroo — July 4, 2009 @ 2:43 pm
Looking to move over to Yii myself after about 1 1/2 years with CodeIgniter
Comment by Jeff Benetti — July 14, 2009 @ 2:34 pm
Just in case you missed this framework during your search, make sure to check out Kohana (http://kohanaphp.com) sometime in the near future.
Love your books!
Comment by Brett — July 29, 2009 @ 9:13 pm
Thanks, Brett, for the suggestion and for the nice words on the books.
Comment by Larry — September 18, 2009 @ 2:58 pm
Good to hear it. I’m hoping to start writing more about Yii soon.
Comment by Larry — September 18, 2009 @ 3:08 pm
Thanks for the feedback! I hope to start writing more about Yii soon.
Comment by Larry — September 18, 2009 @ 3:09 pm
Thanks for your post and that’s a very good question/concern (the limits frameworks impose). Yii is pretty good about working with other technologies, in my experience. For example, you can tap into some of the Zend Framework in a Yii application, if need be. I suspect you should be able to use PHPTAL without too much effort.
Comment by Larry — September 18, 2009 @ 3:12 pm
Hello Patrick. Thanks for the nice words and for the support. Much appreciated. I hope to be writing again soon.
Comment by Larry — September 18, 2009 @ 10:09 pm