Introduction to the Yii Framework

June 18, 2009 — 96 Comments
The Yii Book If you like my writing on the Yii framework, you'll love "The Yii Book"!
This entry is part 1 of 8 in the series Learning the Yii Framework

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.

Yii Requirements Check

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.

If you enjoyed this post, then please consider following me using your favorite social media, the RSS feed, and/or by subscribing to my newsletter. Or go crazy, and buy one or more of my books . Thanks!

96 responses to Introduction to the Yii Framework

  1. 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!

  2. 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.

  3. Really interesting one, i much appreciate. Thanks for sharing the information.

  4. 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…

  5. Looking to move over to Yii myself after about 1 1/2 years with CodeIgniter

  6. 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!

  7. 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.

  8. 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.

  9. 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!

  10. The framework is very hoshy YII
    and what will be new versions?

  11. Thanks Larry. :) i really don’t know yii. so this is a very good headstart.

  12. Thank you for this tutorial, i’ve started to use YII!

  13. 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

  14. 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.

  15. 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

  16. Thanks for helping me to get relief from bouring frameworks

    Thanks

  17. квартири October 3, 2010 at 2:47 pm

    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.

  18. 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.

  19. 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.

  20. 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!

  21. 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!

  22. 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!

  23. 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.

  24. 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.

  25. 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.

  26. Its really useful to learn about yii and MVC framework……….

  27. 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.

  28. 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.

  29. Larry…Very Nice Articles… I too want to start up blogging… :)

  30. Which is the best ide for yii framework

  31. Thanks for the post. I’m a news and using for this framework in my new website

  32. 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.

  33. This is what i am looking for, thanks for helping us learning Yii..
    Regards,
    Donna

  34. Love your articles!!! I’m enjoying it especially I am very new to Yii!

  35. 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 :-P

    Thanks for writing this guide and the MVC.

    If someone know of any “in depth” videos of Yii, pls replay to this post :-)

  36. hi,
    i need to develop social networking software like social engine. i chose yii framework. kindly assist me

  37. i m in new Yii Framework. please give code of CRUD .. i m using Dreamweaver…

  38. Thanks this is a best MVC structure.and easy to learn…..

  39. This post helped me a lot to understand Yii framework. Thanks a ton.

  40. Hi larry! im totally new to this YII framework. and im required to learn and apply what i learn in my final year project. how do i start from here?

  41. Now, I am about to begin a Wonderful Journey

  42. it seems pretty nice :)

Trackbacks and Pingbacks:

  1. Read something about Yii, a Php framework « Huichao's blog - July 16, 2010

    [...] I spent much time following the tutorial of Yii, a Php [...]

  2. Yii 框架快速入门 3.9 Wiki Code Version | #import "Leask.h" - October 1, 2010

    [...] * Larry Ullman’s Learning Yii Series / 语言:英文 / 评级:★★★☆☆ 在线阅读:http://www.larryullman.com/2009/06/18/introduction-to-the-yii-framework/ [...]

  3. PHP Framework for beginners « Data Integrated Entity - October 27, 2010

    [...] The documentation is also nice. And there are also third party authors who has made some beginner tutorials regarding yii. That really made life easier, so why don’t you try out yii. It might also work for [...]

  4. Problem with the Series – Larry Ullman - October 31, 2010

    [...] Learning the Yii Framework [...]

  5. Explaining PHP Frameworks and the MVC Model – RDS Framework Introduction [01] - November 3, 2010

    [...] Introduction to the Yii Framework – Larry Ullman [...]

  6. Introduzione al Framework Yii | Programmando Facile - February 7, 2012

    [...] Larry Ullman Articoli correlati Un pò di materiale su Visual Studio 2010 ed il .NET Framework [...]

  7. Yii Framework: Should you try it? « PageFabric - March 28, 2012

    [...] have no problem extracting and later installing it on your system. There are very good instructions here on how to set up and create your very first application. Overall not complicated at all, but it [...]

  8. Yii tutorials, docs and so on. | Connoissuer's Guild Dev Blog - April 3, 2012

    [...] If you’ve not read them, go read them. Share this:TwitterFacebookLike this:LikeBe the first to like this post. This entry was posted in [...]

  9. Learning the Yii Framework 1 / 8 | 웁스교교주의 이야기 - July 15, 2013

    [...] 문서의 원본은 http://www.larryullman.com/2009/06/18/introduction-to-the-yii-framework/ 입니다. Yii 프레임워크를 초심자가 차근차근 따라가기에 좋은 시리즈인 [...]

  10. Learning the Yii Framework 7 / 8 | 웁스교교주의 이야기 - July 15, 2013

    [...] <!–[if !supportLists]–>1.     <!–[endif]–>Introduction to the Yii Framework [...]

  11. Ressources en français - October 3, 2013

    […] Nous avons la chance que le blob memorandom est traduit l’article original « Inroduction o the yiiframework (en)  » en « Introduction au framework Yii (fr) » […]

Comments are great, but I'd strongly prefer any requests for assistance get made in the support forums. Thanks!