Category Archives: Yii 2 For Beginners

Yii 2 For Beginners New Bonus Material Released Growl and Rating System

I’m happy to announce today the release of bonus chapter 17, the latest update of Yii 2 For Beginners, with 60 pages of new bonus material as of 7:00 pm pacific standard time on April 6, 2015. Anyone who has purchased on or before that date, and that includes today, can login to and pull down the latest version of the book.

As always, updates and bonus material for the book are free for the life of the book. I highly recommend that you update to the latest version to get the most out of your purchase and take advantages of the new features built into the template.

In this bonus chapter, we utilize Kartik’s Yii 2 widgets extension, which includes numerous widgets that we will use, in this chapter, and in future chapters. Specifically, in this chapter, we will be working with the StarRatings widget and the Growl widget.

We create a functioning rating system for our Faqs, so users can rate them. You can use this as a prototype for any model that you want to have ratings for, so this is very handy.

We also implement the Growl widget, which takes a plain flash message and animates it, so you can add some life to the frontend.

We also do one cleanup task, which is to add a checkbox to the signup page, so we can have users agree to the terms of service. It’s a little detail, but since almost every application will require it, we provide a working solution.

And finally in this chapter, we return to the data-driven carousel to make it more adaptable to mobile browsers. Now, when the browser is scaled down, the carousel will switch to a mobile image that is also 100% controlled by our backend admin UI.

I felt it was really important to get this detail right. And after seeing some great comments and reviews on my book, I decided we couldn’t settle for second best, so I made sure we got what we needed. I think you will love the results.

Everyone appreciates the efforts of those who wrote in with typo notices, it makes for a better reading experience for everyone, so thanks to everyone who contributes.

I will continue to do my best to develop the template and bring you more bonus material. Any efforts on your part to help spread the word on the book will be greatly appreciated. Thanks again for purchasing the book and for your support.

Yii 2 For Beginners Update Image and File Management

I’m pleased to announce today the latest update of Yii 2 For Beginners with over 100 pages of new bonus material as of 3:00 pm pacific standard time on March 26, 2015. Anyone who has purchased on or before that date, and that includes today, can login to and pull down the latest version of the book.

As always, updates and bonus material for the book are free for the life of the book. Just login into your account and pull down the latest version. I highly recommend that you update to the latest version to get the most out of your purchase and take advantages of the new features built into the template.

I worked really hard to get this bonus material out to you, it covers a lot of new techniques, including use of Yii 2’s scenarios. The page count of the book is now over 600 pages. For those unfamiliar with the book, we take you step by step through setup and installation and then onto coding a reusable template.

The book features:

  • Setup and install
  • A Working user model
  • User registration and login
  • User Profile
  • Forgot password recovery
  • Frontend and backend/Admin separation
  • Helper classes
  • Access control
  • Free/Paid content control
  • RBAC with backend UI
  • JUI DatePicker
  • Facebook Social Widgets
  • Font-Awesome Implementation via CDN
  • Autoresponder Email for signup
  • Facebook Login & Registration with one click
  • Image Upload and Management
  • Multiple Social Auth Providers
  • Custom Data-driven Carousel Widget

More will be coming…

In this bonus material from this release, we cover basic image management, which includes uploading, updating and deleting both images and thumbnails. We also build a dynamically-driven carousel widget that is controlled by our admin UI.

You can set things like font size of the caption, show captions or not, show controls or not, etc., all from the backend UI.

This means you can hand total control of a key marketing area on the template over to the client and they will love that. Marketing is very important to clients and site admins and they will appreciate this level of detail.

I also went back to chapter 14 and added in the formatProviderResponse method both Google and Linkedin, which I had previously omitted. I’ve also included step by step instructions for setting up Google and Linked Apps, including screenshots to help you along.

While I was busy writing this update, some of our readers left positive reviews and comments on, this blog, and the feedback section of the book’s landing page on This really inspired me to go all the way with the data-driven carousel and see how cool we could make it. I hope you like the results. I also hope you will contribute some positive comments and reviews of your own.

A number of readers also wrote in with typo notices and I jump on those immediately, with the turnaround within a few hours to 1 day. Everyone appreciates the efforts of all involved, it makes for a better reading experience for everyone.

Working on the Yii 2 framework is an amazing journey and I really enjoy sharing it with you. Thanks again for supporting our efforts.

Yii 2 Pretty Urls and Sluggable Behavior

I’m happy to announce another bonus chapter has been added to Yii 2 For Beginners. Everyone who has purchased the book gets free updates for the life of the book, just log into your account and pull down the latest version.

In this update, we focus on having our application use pretty URLs and on using slugs. Pretty Urls get rid of the ?r= syntax and the index.php part of the url. Slugs are strings that get attached the url to make them more visible to a search engine, like so:

It’s fairly easy to implement, but there are a lot of little things to get right and we go through all of them for a complete solution.

Also, as I mentioned in my last update, I made sure we were 100% compatible with Yii 2.0.3, which has just been released. I jumped on that on the day of release and everything went smoothly with the update, so no surprises there.

I also included in this update the correct jui DatePicker solution, which works with this update to the framework, so my workaround is no longer needed. A thoughtful reader was kind enough to send in a suggestion that includes setting the year as a dropdown list, so it’s easier to select the year, when you are picking the date. You will love it. That update is at the end of chapter 8.

I’ve decided to write smaller bonus chapters, so I can release the material more quickly. I’m working on a chapter dedicated to Yii 2 authclient and will have that done by the end of the month. This will allow a one-click login or register via facebook and other social networks. It’s an awesome feature of the framework, another reason to absolutely love it.

Thank you to everyone who wrote a positive review at Your comments are helpful to people who are thinking about learning the framework.

For anyone who does want to learn Yii 2, the core book is finished and will get you up and running quickly with a reusable template that you can use for your production projects. Since I use the template myself, I’m continuing to add to it, and as I do that, I continue to release bonus material in the book. It’s all free for the life of the book.

Thanks to all the programmers from around the world who are supporting my blog, my book, and participating in the polls. Over 400 people have already voted in the latest Yii 2 vs. Laravel 5 poll and we’re getting some interesting results. Thanks again and see you soon.

Yii 2 for Beginners Bonus Material Released

I’m happy to announce that the first chapter of bonus material for Yii 2 For Beginners has been released. The book has gone from a previous page count of 290 pages to 412 pages, so this is a very substantial addition.

Everyone who purchases Yii 2 For Beginners gets FREE updates for the life of the book. Just log into your leanpub account and download the latest version. I’m planning to continue to do updates and write bonus material for the foreseeable future. There are a number of reasons for this.

1. Striving For Excellence. I want to build a reputation for excellence and quality as a technical writer and I’m deeply committed to this project.

2. Framework Updates. I want to stay on top of Framework updates. The latest version of Yii 2 is 2.0.2, released two days before I released my bonus material. This meant I had to frantically rebuild the entire application from scratch, following the directions in the book, just to make sure everything is working. There were a few minor changes and I caught them all.

3. Maintaining the Template. The template we build in the book is a useful template for creating sites, including my own. I need to stay on top of version changes as much as anyone else, and as I’m doing that, it only makes sense to share the version improvements with my readers.

4. Sharing the Journey. Working with Yii 2 is an amazing experience that I would like to share with my fellow programmers and engineers. Working on this book is a way to express my passion for Yii 2 and grow at the same time.

I’d like to thank everyone who emailed or commented for the wonderful feedback, especially the positive reviews on GoodReads.

I really appreciated the notices on the typos as well. Most of the typos are trivial, but I still want to correct them. I won’t be satisfied until the book is 100% free of typos and I think we are probably close to that now.

I know for a fact the code is 100% working. The build I did over the past few days was from code taken directly from the book, via the gists, and it works perfectly. For anyone who doesn’t know, a gist is a code snippet hosted by Github.

Copying code from a PDF can be a real pain and I had multiple requests from readers to post the code. So, in response, I have supplied a gist for any block of code that is over 3 lines. The gists are inline in context in the book, so you just click a link and you get the relevant code. This means you don’t have to bookmark or organize anything, it’s already done for you.

New in this Update

1. Yii 2 Version Update. I already mentioned that Yii 2.0.2 was released a couple of days before I finished and this was perfect timing for me. The three notable changes concerning the template were:

A. The advanced template doesn’t ship with a role column on the user table like it did before.

B. Forgot password methods changed.

C. The typo generated by Gii when creating a controller has been fixed.

So nothing here required any dramatic changes on my part, just some instructional text had to change. This worked out well. I should point out that I ran into an unknown problem when I tried to run composer update, composer just got hung up. Another user experienced an issue as well.  My work around was to build from scratch. I had to do this anyway because I had to test everything 100% on a new build. My code is 100% compatible with both Yii 2.0.1 and Yii 2.0.2. If you do a fresh build, everything will work perfectly.  So that’s the good news there.

2. Fine Tuning Core Book. Doing an update always gives me the chance to go back over the instructions in the book to fine tune them for maximum clarity. I also catch typos, and obviously any bugs in the code. I can’t claim a 100% on typos, but I know the code is 100% working because I use it myself, and like I said before, I just rebuilt the application from scratch using the code from the book. Nothing in the core book changed in terms of how we were doing things, so nothing of substance to report.

For those that don’t know, the core book takes you through setup and install of Yii 2, then onto building a reusable template that also includes:

  • A Working user model
  • User registration and login
  • User Profile
  • Forgot password recovery
  • Frontend and backend separation with Admin UI
  • Helper classes
  • Access control
  • Free/Paid content control
  • RBAC with backend UI
  • JUI DatePicker
  • Bootstrap Mobile Responsive CSS
  • Facebook Social Widgets
  • Font-Awesome Implementation

The bonus material extends the core template with useful features, at the same time, we continue learning more about the framework.

3. Autoresponder. In the bonus material, we build an auto-responder. This is similar to the one I talk about on the blog, only this is the full set of instructions, with tested code. So again you know it’s 100% working.

4. Dropdown Navigation. We implement dropdown navigation links from my own solution. It’s quick and easy and you will love it.

5. We build an FAQ model for the template. FAQs are common to most sites and this will come in handy. This implementation has a full frontend/backend UI with extensible data structure. It is very useful code.

6. Components. We build the component application structure and show you how to create a component in the Advanced Template, including all the config details.

7. Custom Widget. Creating widgets are easy once you know how. This is a very powerful piece of the Yii 2 architecture that you will fall in love with. I will be blogging more about this in the future, it’s just amazing.

8. Closing A Potential Hole. There is no hole in the code if everything is coded correctly, but what happens when someone makes a typo? This section makes a nice adjustment to the ValueHelpers class that helps prevent accidental exposure.

9. CDN. We finish the first round of bonus material with a simple config implementation to use CDN for the core CSS and JS assets. This can dramatically increase the speed of the application.

All in all, it’s over 100 pages of new material. To reiterate, this is all 100% FREE to anyone who has previously purchased the book. Just log into your account to download the latest version.

I really had to work hard to get this to you in a timely manner. I put another book-writing project on hold, so I could make sure I live up to the commitments in supporting Yii 2 For Beginners. Now I need a few days off before I collapse from exhaustion.

Thanks again to all the readers from all over the world who were supportive with positive comments and are supporting my work. Your feedback motivates me to keep going. For example, on GoodReads, we are averaging 4 out 5 stars, with some nice comments even from the lower rating, which came before the additional 100+ pages of material was added to the book.

Please keep the comments, links, positive reviews, and word-of-mouth-referrals coming, they are greatly appreciated. See you soon.

Yii 2 Framework Development

Welcome to anyone from around the world who is just discovering this blog. It’s amazing how programming transcends international boundaries, which were always artificial anyway. Over 1000 developers have taken part in the polls on this site and I would encourage you to participate, either by voting or commenting or doing both, let your voice be heard.

Also, thanks to everyone who has bought Yii 2 For Beginners, I appreciate your support.

I recently discovered this Youtube video on the development of the Yii 2 framework.

I would strongly encourage you to watch the video, it’s about 3 minutes long. It shows a time-compressed evolution of the framework, starting with the first development by Qiang Xue, quickly followed by Alexander Makarov (Samdark). Then as the video progresses, you see some of the other Yii 2 stars such as Carsten Brandt (Cebe) and eventually Kartik V. popping in to make some contributions. Over 300 developers contributed to the making of Yii 2.

This video is just fascinating on so many levels. For one, you get to see how Yii 2 starts, with Qiang Xue laying out the base classes. Who knew that validators were such a big group of classes? If you ever wondered what the founder of Yii actually did, here it is in graphic detail, played out against some pulse-pounding music.

You also get to see how Yii 2 grows as a codebase, and how that intensifies as more developers come into to contribute. Entire branches of the framework seem to suddenly emerge as the energy pours in from the community. Each update is represented by a small spotlight effect or maybe it’s a small lightening bolt. Anyway, it’s pretty cool.

The representation of the project is made possible through Google’s Gource software, which extracts the updates from Github and compiles them into a video.

I’ve already done a lot of comparisons to Laravel on this site, but there is even a version for Laravel. One myth that dispelled for me was that I thought that Laravel was only one person, but you can see from the video that there were many people involved. Also, I think the video gives the impression that Laravel is lighter than Yii 2, but I think this is because Laravel depends on Symfony 2 and Doctrine, and obviously, that is not included in the Gource.

In some ways, the two videos draw a nice contrast between the two frameworks. The gource for Yii 2 is the entire framework, (up to the date it was made), the gource for Laravel can’t be because it’s layered on other dependencies. So no matter how gracefully that is handled, and Taylor Otwell did a great job, no doubt about that, it still is not quite the same as a single integrated entity like Yii 2, where the parts fit together seamlessly.

At this point, I can’t really claim objectivity, since I wrote a book on Yii 2, but I do think these differences matter. For example, Yii 2 really sticks to the MVC pattern in an intuitive way. You get folders named, Models, Controllers, and Views. You get files with names like Site Controller.php in the controllers folder. Yii 2 does the routing for you, whereas in Laravel, and Symfony 2 and Zend 2 for that matter, you have to set all the routes manually, and that is every route through the site explicitly declared, which is a big pain.

I think you can see from the video that a lot of effort went into the creation of Yii 2. So let’s conclude with another thanks to all those who contributed to the making of such a great framework. Feel free to let us know what you think, we would love to hear your comments, thanks for participating.

Yii 2 Mailer and Auto-Responder

Yii 2 Mailer Tutorial

This Yii 2 mailer tutorial implements a method to send response messages via email to users of the application. This is sometimes called an auto-responder.

This tutorial is not included in my book, Yii 2 For Beginners, but I thought it would make a great addition to the template that the book builds.

This is not a short tutorial, even though in reality, there isn’t much code. The reason is that we are not only going to talk about how we are approaching our methods, but also why. The why is very important.

A couple of years ago, I discovered a youtube video that talks about how important “why” is, and ever since then, I’ve made it part of my lesson plans.

Also, my blog is geared toward beginners, so I take great pains to explain in great detail what we are doing, what decisions we made, and why we made them. We want to not only advance your understanding of Yii 2, but we also want to learn a little about design principles and their application.

The goal, as always, is to make the final code as pretty, thin, and usable as possible. Ultimately, the email trigger method will be in a controller, so we always want to keep that code as clean as possible.

Yii 2 For Beginners

I’ve been studying the PHP framework scene for the last 3 years in depth. I’ve done comparisons on all the major frameworks, including Laravel, Symfony, and Yii, and in the end I chose Yii 2 as my framework of choice. You can see a little of the history of this, my blog, Yii 2 vs. Laravel, talks about discovering Yii 2 when it was only in Alpha. After trying all of the other frameworks, watching countless videos and reading many books, I settled on the one PHP framework that had no book whatsoever to help beginning programmers.

I always felt, if only there were a book for beginners, I could learn this quickly, I just needed someone to point the way. Instead, I learned piecemeal by gleaning tips from the forum, the guide, and from the fact that the framework was very intuitive. Even though I made progress, I still found myself wishing for the book. Learning this would be so much easier, if I just had a book!

How ironic is it that 6 months later, I end up writing the book I yearned for? Well, I kept very detailed notes on my development and I noticed over time that I could organize them into a book. I just had to be willing to sacrifice the time, which could have been spent developing actual sites, which instead had to go into writing lessons for other people.

It was a tough choice, but I chose to write the book, it seemed like a worthwhile endeavor. It turned out to be an incredible challenge. It pushed me mentally and physically. I spent long nights picking apart every detail, striving for excellence and clarity, so that others could more easily understand how to use Yii 2.

I started out fairly objective about PHP frameworks, simply looking for the easiest framework that could support robust development. Although I had a lot of respect for Symfony, I felt it was overly complicated and bloated. I didn’t like having to learn Yaml, even though it’s not difficult. I didn’t like having to write every route through the site. I didn’t like what I saw of doctrine. And I didn’t like the fact that alhough they had beautifully written documentation, there was no real effort made to service beginning programmers. This has a downside for businesses that have to hire programmers because it then becomes more expensive to train programmers. So in that sense, Symfony is an expensive discipline.

Laravel, on the other hand, went out of it’s way to service beginners and I loved that about it. The problem was, I just never felt like they were helping me build anything. None of the examples I found at the time, for example, dealt with a robust user login and verification. And without that, what kind of site do you have? And then, because it’s built on top of Symfony, it brings in Symfony’s bloat and performance issues. The other programmers at my company didn’t like it either. It didn’t seem robust enough to them, though in fairness, I should say that it was very new when we were doing those comparisons.

I turned to Yii 1.1.14 because my company had settled on it and the programmers loved it, but I found it difficult to grasp personally. In retrospect now, since I came up to speed on Yii 2, I understand 1.1.14 a lot better, but at the time, it seemed so difficult to use. The reality was that it was difficult to learn, but easy to use. You just had to be at a certain level as a programmer to get that. I hated that about it.

Then I discovered Yii 2 and everything changed. Whereaas Yii 1.1.14 was old and ugly, using old array syntax, no namespaces, etc., Yii 2 was a beacon of modern PHP. And being beautiful architecturally in this case is not just a superficial thing, it is extremely versatile and useful, way more intuitive than it’s predecessor. I have so much enthusiasm and passion for it, that I want to share it, and this was one of the motivations to write Yii 2 For Beginners.

Also, I know that right now, a beginning programmer is wandering around, wondering how to go from novice PHP programmer to framework developer. That’s a tough transition to make. I know just how frustrating it is. I want to help.

There are many, many, more advanced, and more qualified programmers than me, who apparently don’t have time or the interest to bring it down to the beginning level, so that task has fallen to me, at least in the short-term. I didn’t plan for this to happen, I’ve just found myself in these circumstances. There is no other beginning Yii 2 book out right now. So I did the best I could and I put a lot of effort into making Yii 2 as understandable for a beginning programmer as possible.

I realize that by being so into Yii 2, I no longer have objectivity to the other frameworks. That was bound to happen. I had to pick one. And when I get into something, I go all in.

So Yii 2 For Beginners is out now. I’m really glad I could contribute something to help beginners harness the potential of Yii 2, it really is an amazing framework.

Now that the book is done, I’m looking forward to getting back to application development, I hope to have a new site done in January. After that, I’m going on vacation, if I’m still breathing…

I will continue to support PHP and Yii 2 beginners on this blog, and hopefully continue with the polls, which people seem to find interesting. Over 1000 developers have voted in my polls. I hope to continue to develop this blog as a resource for the community.