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
hi, I am a codeigniter programmer. Is this framework better than codeigniter? I mean in terms of performance….
Comment by endless beginning — March 15, 2010 @ 4:53 am
Bookmarked for future reference
Comment by James @ be the top of google . com — March 16, 2010 @ 11:52 am
The short answer is: I don’t know. The long answer is that I don’t put much faith in benchmarks comparing software. I’m sure there are some numbers that show Yii is faster, others will show that CodeIgniter is faster. When I decided to start using Yii, I saw impressive benchmarks in Yii’s favor, but I wouldn’t use that as a basis for choosing, let alone switching to, any particular framework.
Comment by Larry — March 16, 2010 @ 5:24 pm
Great. Thanks!
Comment by Larry — March 16, 2010 @ 5:24 pm
Really enjoy your info about Yii. I feel that Yii is a great framework and you explain it in the easiest way I’ve seen. Hope you will write more about this framework. You provide a great service.
Comment by Jeff — March 26, 2010 @ 9:19 am
Thanks for all the nice words. I will continue to write about Yii. Let me know if you have any specific topics you’re curious about.
Comment by Larry — March 27, 2010 @ 9:24 am
Since you asked, a few topics I would love to see are 1) How to use WHERE SQL conditions to a query 2) How to use Form Builder and 3) How to use a User table. I know the first two you already mentioned you might write about. Thanks Larry!
Comment by Jeff — March 30, 2010 @ 11:42 am
Thanks for the suggestions. I’ll see what I can do!
Comment by Larry — March 30, 2010 @ 7:09 pm
The framework is very hoshy YII
and what will be new versions?
Comment by dima — April 4, 2010 @ 2:07 am
Thanks for your comments. Unfortunately I have no idea what you’re saying.
Comment by Larry — April 4, 2010 @ 8:33 am
Thanks Larry.
i really don’t know yii. so this is a very good headstart.
Comment by Mars — April 4, 2010 @ 8:39 pm
You’re welcome. Thanks for the nice words and I hope Yii works for you.
Comment by Larry — April 5, 2010 @ 11:54 am
Thank you for this tutorial, i’ve started to use YII!
Comment by футбол — May 1, 2010 @ 4:52 am
You’re quite welcome. Thanks for the nice words and good luck with Yii!
Comment by Larry — May 1, 2010 @ 5:55 pm
Hi,
Can you please tell me how to set a global variable in Yii framework? I have to set a global variable and it should available to all of my view pages.I tried to set it in index.php, but its not working. Please give me an instruction about this. I am in deep troble. PLease help me….
Regards,
Rejeesh
Comment by Rejeesh — May 3, 2010 @ 1:52 am
If you search the Yii site for “global variable”, you come across this post.
Comment by Larry — May 4, 2010 @ 8:23 am
Hi, Larry thanks for your great tutorial and everyday i go through it. I have a question on how can i run yii command on ubuntu desktop with lamp server o get skelton application ? Please help thanks.
Comment by bonny — May 19, 2010 @ 11:09 am
Thanks for the nice words. As for running yiic on Ubuntu, the instructions would be essentially the same as those for Mac OS X. You just need to cd into the framework directory, then execute the yiic webapp command.
Comment by Larry — May 20, 2010 @ 8:47 am
Thanks Larry for your help on running yii on ubuntu, the following steps worked for me and I would like to share with anybody who might be in need of it.
For the Ubuntu guys this is how I created my yii skelton application ubuntu 10.04 LTS running Lamp server.
First run sudo apt-get install php5-cli on terminal(comman line)
Second create a folder in www or htdocs called test(for me but you can create your own)
Third run cd /var/www/yii/framework on terminal(command line)
Lastly run php yiic webapp var/www/test
And that is it, you have your yii framework skelton application in ubuntu to start modifying.
Note this was done on ubuntu 10.04 LTS
Comment by bonny — May 21, 2010 @ 9:19 am
Great. Thanks for sharing!
Comment by Larry — May 23, 2010 @ 8:31 am
[...] I spent much time following the tutorial of Yii, a Php [...]
Pingback by Read something about Yii, a Php framework « Huichao's blog — July 16, 2010 @ 9:56 am