In 2009, I had three decent-size Web sites to develop, 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. At the time, the Yii Framework was still quite new, and there are still bugs to be worked out (for the more advanced stuff), but Yii works so well that it’s very easy to use. In this first of several posts on the Yii Framework, I just discuss setting up and testing Yii.
(Note: In October 2010, I’ve updated this entire series to reflect changes in Yii since this series was written, and to take into account feedback provided through the comments. Some outdated material will be crossed out, but left in to reflect how things have changed since the series was begun in June 2009.)
The first thing you need in order to use the Yii Framework is access to a Web server with PHP installed, of course. 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 1.1.4. The file you download will be named something like yii-version.release.ext and is only around 2MB. Expand the downloaded file 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: a blog, the game Hangman, a basic “Hello, World!”, and a phone book. The demos are 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 your 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 walk you through developing a Yii-based application. Use the series links at the top and bottom of this post to navigate the series. You may also want to read my posts on the MVC architecture.











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.
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!
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.
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.
Really interesting one, i much appreciate. Thanks for sharing the information.
Thanks for the feedback! I hope to start writing more about Yii soon.
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…
Hello Patrick. Thanks for the nice words and for the support. Much appreciated. I hope to be writing again soon.
Looking to move over to Yii myself after about 1 1/2 years with CodeIgniter
Good to hear it. I’m hoping to start writing more about Yii soon.
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!
Thanks, Brett, for the suggestion and for the nice words on the books.
hi, I am a codeigniter programmer. Is this framework better than codeigniter? I mean in terms of performance….
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.
Great. Thanks!
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.
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.
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!
Thanks for the suggestions. I’ll see what I can do!
The framework is very hoshy YII
and what will be new versions?
Thanks for your comments. Unfortunately I have no idea what you’re saying.
Thanks Larry.
i really don’t know yii. so this is a very good headstart.
You’re welcome. Thanks for the nice words and I hope Yii works for you.
Thank you for this tutorial, i’ve started to use YII!
You’re quite welcome. Thanks for the nice words and good luck with Yii!
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
If you search the Yii site for “global variable”, you come across this post.
You have to define the global variable in Controller.php inside components.
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.
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.
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
Great. Thanks for sharing!
Thanks for helping me to get relief from bouring frameworks
Thanks
Thanks Larry, I needed a quick tutorial for Yii, and your tutorial is excellent. I think this will be the new framework I’ll use.
Thanks! I hope you like it.
Very best for beginners of Yii
I don’t think you really keep abresat of what’s happening in the PHP world today. Zend is notoriously tough to pick up and learn mainly owing to the really shitty documentation. If someone is just starting with frameworks, Fat-Free Framework is a very good choice IMO. Or the Silex microframework. These let you build your own systems on top of a slim and light layer which provides the essentials. Zend, more than a framework is a huge set of components somehow tied together. And for most projects, it’s overkill. BTW, CakePHP has reached release 2.0 which looks quite promising.
I’m not exactly sure what you meant, but just so you know, “I don’t think you really keep abresat [sic] of what’s happening in the PHP world today.” is an amazingly stupid thing for you to say. Again, not sure what you meant to convey, or to whom, but you sound like an idiot there.
Quick question,
I’m currently a student and novice/intermediate level with php and trying to deepen my php skill.
Will using Yii framework help me learn more deep end of php or will I become more of slacker using framework?
Good question. Using a framework, really using a framework, actually requires a higher level of knowledge, especially OOP knowledge.
Thank you for your awesome posts Larry. You are very helpfull and these posts are a treasure for inexprerienced users who want to learn yii framework!
Thanks, Giorgos. Much appreciated.
Hello Larry,
MVC and your post about it is great. I usually write my own object oriented php code for webpages. but now, I am facing a bigger long time in any direction extending project and decided to finally use a framework. I am testing the most common frameworks and cms’s. like codeigniter, symfony, modx, drupal and right now I am hacking around yii. I appreciate your documentation about it. my question is, what do you think about silverStripe? and the framework behind it, called saphire? it made a damn good impression to me, except of some performance issues… on top of that, whats your opinion, which framework will serve us the best for the future?
Thanks for the nice words, Silvano. As for SilverStripe and Saphire, I’ve heard of SilverStripe, but have no personal experience with it. As for what framework will serve “us” best in the future, that’s an unanswerable question. The right framework for me may be different than the right framework for you. And the right framework for project X may be different than the right framework for project Y. Assuming a framework is right for the project at all. My advice is always to use the framework that works best for you when you think it makes sense to use a framework at all.
Right, I kinda expected this answer *lol*! At least you’re article about MVC helped me to stick to a MVC architecture for the future, since it is the most natural for WebPages and it also forces me to divide code and view properly. Before I used Smarty but now Yii makes much more automatically, which is good for a programmer and a good programmer is a lazy programmer, right? Thank you again and if I find a book which serves my need, I am going to tell my employer to order some Larry Ullman book. Please, keep it up!
Thank you, Silvano. Book recommendations are much appreciated!
Hi Larry,
I loved the article. To be honest, there are a lot of people saying a lot of good things about Yii but not enough external resource, I mean good ones other than the website and thank God your bunch of articles. Codeigniter’s user guide is a lot better.
Since there are only 2 books on Yii, have you thought of writing one? I have 2 of your PHP & MySQL books and I’ve glanced through some of the rest and you explain things really well and your examples are relevant. I’m actually finding it a little harder to pick up Yii. It’ll be a little helpful to have a good reference. No disrespect but I heard the current book is not as good as it should be.
I’ve blabl-ed a lot I apologize, I just wanted to thank you for the articles.
Thanks for the nice words on the article and on my books. It is appreciated. I am actually hoping to write and self-publish a book on Yii. If you follow the blog or my newsletter, you’ll get updates when and as that happens. Thanks again!
That’s great news
I wish you the best of luck with achieving your book goal. I’ve subscribed to your newsletter and I would definitely keep both eyes out for your book. Since my last comment, I’ve done much better. 90% of the basics came from your articles. There are quite a few fantastic ones on here. Thanks again!
Thanks, Hassanin. Much appreciated!
Hi Larry,
Do you still endorse Yii? Just out of curiosity (and I am sure you have at least browsed through the documentation) how do you feel about other popular frameworks such as ZEND/Kohana/CI?
Hello Helmut. Absolutely I still endorse Yii. I haven’t done any major Web projects that require Yii in a little bit, but I have a couple coming up and plan on using Yii. I’ve not used Kohana, but heard good things. I’ve not used CodeIgniter, but have heard good things. I’ve used Zend before, and didn’t care for it at all as the basis of a Web site. I do still use Zend components as needed (kind of like PEAR), and really appreciate it in that way.
How did you decide to use Yii rather than the “other”, I personally have use them all and I do like them and it is hard to stick just to one. It looks like there is always something the “other” has that the one you are using doesn’t have. If you know what I mean.
Looking forward to hearing how your new projects go.
Hello Helmut. Thanks for the question. I use Yii largely because it feels right to me and I found I was able to use it without much confusion or effort. I do have a criteria for frameworks that they require (i.e., only run on) PHP 5, which rules out some options. The first PHP framework I used was Zend, and I never got the hang if it. It always felt clunky and it seemed like I had to do a lot of unnecessary work. The first Web framework I used was Rails, which I like a lot. Yii reminds me of Rails, which is part of the reason I took to it. Not to say that other frameworks aren’t good or better for other people, though.
Hi Larry,
I used your php books in 2004/5(i think) and have alot gain experience with PHP at that time. Are you planning to write about using PHP Framework Books, especially on Yii Framework?
Hello Koko. Thanks for your interest in my work. Yes, I am planning on writing a book on the Yii framework, but it’ll be a couple/few months before I get to it. I’m working on a JavaScript book now, that I’d have to complete first.
Larry ,
I am going thru your tutorials;
1. Great job brother.
2.Yii requirements checker showed me all the passes – as per your screenshot
3. Executing ./yiic webapp /var/www/html worked with no issues BUT
4. WHEN I http://localhost , i get;
PHP Error
date() [function.date]
It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier
5. SOLUTION – in php.ini date.timezone = must be set. I set mine to “Asia/Kuala_Lumpur”
6. Thanx.
Thanks for the nice words. As for that error, that’s an issue with later versions of PHP. It’s not particular to Yii, just so you know.
Its really useful to learn about yii and MVC framework……….
Thank you for the great tutorials.
I was drawn here because I have a project to do for school. Our team is leaning towards using the yii framework for the first time.
Over the duration of 10 weeks, our team has to develop an application for a company that wants to keep track of its clients and book events. Our team is composed of three 3rd year software engineering students with 3-4 years of experience with PHP and 3 other students with 1 year of experience.
The ultimate question is:
Given this context, while dedicating on average 10-12 hours per week per person on just coding and learning yii;
A) Would it be feasible to take on learning this framework which is new to all 6 team members?
OR
B) Would it be too much of a liability and we should save learning yii for a separate project (perhaps a 8 month project coming up next semester)?
Thanks for the nice words, Patrick. In a team project, using a framework is even more of an advantage as it standardizes all the code and processes. If you weren’t to use frameworks, you’d have to spend a lot of time deciding how code would be written, who would do what, etc. So I would definitely consider Yii. If not, then you really need to plan everything out well. And you should look into using a version control system regardless.
This series is very helpful for me! Thank you a lot.
May I translate these documents into Korean and share with my team members?
Thanks for the nice words and for asking about translating them. Please do. All I ask is that you include a note something like this:
–
This is a translation of a work originally written in English by Larry Ullman (www.LarryUllman.com). It is translated and republished with his permission.
–
Something like that (but in Korean, naturally). I’d just like it to have my name, Web site, and that link to my Web site. Since the articles are long, I’d prefer to have that note (or whatever you write) at both the top and bottom, if that’s not too much to ask.
Please share the link with me when you post the translations so that I can share the link on my site.
That sounds good. Thank you for your kindness!
You can find the translated posts at [ http://lazygyu.tistory.com/tag/Yii ].
Also you can find at [ http://lazygyu.tistory.com/90 ] and [ http://lazygyu.tistory.com/91 ].
Only two translations yet, but I’ll translate entire posts in this series soon.
Of course, I did write links to this page in my posts.
Again, thanks for your permission to translation and share.
Thanks for letting me know and thanks for your efforts. Hopefully that will help spread Yii!
Larry…Very Nice Articles… I too want to start up blogging…
Which is the best ide for yii framework
I don’t know what the “best” is, but see these threads: http://www.yiiframework.com/wiki/?tag=IDE
Thanks for the post. I’m a news and using for this framework in my new website
I am trying to use Yii to write a stock control system for a shop including invoicing, etc. Do you think it is possible with PHP, mysql and Yii framework? I am quite new at PHP and have not mastered it yet. But I think it is a very powerful tool and would like to use it rather than something else. Will it be possible to easily integrate Javascript, Jquery and modify the code generated by Yii? Thank you for your answer.
This is definitely possible with PHP, MySQL, and Yii, but you should master PHP and MySQL before trying to do it with Yii. It’s definitely possible to integrate JavaScript and jQuery into the Yii application, assuming that you’re completely comfortable with PHP, JavaScript, jQuery, and Yii.
This is what i am looking for, thanks for helping us learning Yii..
Regards,
Donna
Love your articles!!! I’m enjoying it especially I am very new to Yii!
Thanks, Neil!
There is no plan of making a youtube for people having trouble reading and getting all this? Im going to keep struggling, will probably be done with it before a youtube might be finished tho
Thanks for writing this guide and the MVC.
If someone know of any “in depth” videos of Yii, pls replay to this post
No plans currently for doing any videos, but thanks for asking.
When I watched youtube tutorials there were a lots of installations and configurations for the development environment….are all those stuff necessary to start using the Framework?
Yes, you’ll need to install the framework, create a site shell, and then edit the configuration file.
hi,
i need to develop social networking software like social engine. i chose yii framework. kindly assist me
i m in new Yii Framework. please give code of CRUD .. i m using Dreamweaver…
Thanks this is a best MVC structure.and easy to learn…..