Yii 2 FAQ

As part of our ongoing series to build a reusable template in Yii 2, we are going to do a tutorial on our FAQ model. We will focus mostly on our frontend implementation, but we will also be talking about the data structure, differences in the frontend and backend, and we will make use of Yii 2’s ArrayDataProvider, a very handy iterator for use in sending data to the views.

Ok, so if you are just joining the blog, and have not yet gotten started on the basics, I recommend my book, Yii 2 For Beginners, which is a step by step guide to get you installed, up and running, and into development. The book is oriented toward beginning programmers who are just making the transition into frameowrk development and in addition to explaining how things work, we take care to explain why things are done they way they are done.

This blog is meant to be an extension of the book, great for tips and tutorials, which will be included in the quarterly updates of the book. Anyone who purchases the book is entitled to the free quarterly updates for the entire life of the book, so hopefully you can take advantage of that.

One of the other big themes I like to explore is the idea of working for a client and keeping the client satisfied. I’m just going to assume that you will be building applications for a client who is difficult to please.

When we think of FAQs, we think of simplicity, just questions and answers, and simplicity typically serves us well. But if we think about our demanding client and things that are important to them, the order of the questions, for example, might be very important. And obviously, the order they want will most likely not match the ascending or descending timestamp for creation or alphabetical order.

In most cases, the client won’t even know what order they want the questions in until long after the project has been developed. So what we need is a way for them to determine the order via backend UI. Now this is really simple to do of course. We just add a faq_value field, with a data type of int, that we can sort either ascending or desceding depending on our preference.

For example, in an ascending sort, an FAQ with a value of 10 will be higher on the list than an FAQ with a value of 20. And as long as we give the client a method for changing the value of an FAQ from backend UI, the problem is anticipated and solved before it ever becomes an issue.

Yii 2 Test Controller

Welcome back to our beginner’s journey in Yii 2. I’ve made it my mission to help programmers at the beginning stages of framework development to learn about Yii 2, one of the most intuitive and powerful PHP frameworks available.

For those just getting started, I recommend my book, Yii 2 For Beginners, which will take you step by step through setup and onto coding. The book focuses on learning Yii 2 and building a reusable template that can serve as a starting point for your projects. It shows you how to do things, but also explains why we do things the way we do.

This blog serves as an extension of the book, where I share development tips and tutorials that will help you learn more about Yii 2. I also roll out new pieces of the template, which will be incorporated into the book on a quarterly basis. Anyone who purchases the book, gets free updates for the life of the book, so please take advantage of that, if you can.

Today’s tutorial is about implementing a simple test controller to help you with development. This tutorial is for beginning level programmers.

Sometimes in development, you want to isolate a block of code and play with it, without cluttering up your other code. Now version control should always be there if you made a huge mistake, but you really don’t want to be relying on that to step backwards unless you absolutely have to.

So what we’ll do is create a controller named test. We start by navigating to Gii, and clicking on Controller Generator. We don’t have to worry about creating a model first, since we don’t have one, our test controller will be used to play around with different bits of many models, and we will pull in the models via use statements as we need them.

If you don’t know how to use Gii or haven’t started using the framework yet, I would recommend learning that first before moving on to this tutorial. We’re assuming here that you know how to use Gii, which is Yii 2’s built-in code generator.

Ok, so back to the create Controller form on Gii. Pop in test into the first input field to name the controller. We will create just one action for now, Index. Then check to see if the namespace is set to where you want it to go.

In the advanced template, which is what I use because they provide a working user model, I’ve created a test controller for both frontend and backend, though that’s not really necessary. It just helps me when I’m thinking of things to intuitively associate a backend test controller with things I want to try in the backend. Also, sometimes, I leave code in place in the test controller while I’m thinking about whether or not I want to use it.

Ok, so when you hit preview on Gii, it will show two files, the controller and the view file. Once we click generate, it will also create the directory to hold the view file. So let’s do that now.

If all went well, you should have a TestController.php file in frontend\controllers and a frontend\views\test\index.php file.

Yii 2 Behaviors Blameable and Timestamp

One of the things that really made me fall in love with the Yii 2 framework is the intuitiveness of the behaviors method. Behaviors in Yii 2 is a very flexible concept that works on both models and controllers.

On the models, I’ve used two behaviors, timestamp and blameable. Both add a lot of efficiency to the model and prevent code duplication. I’ve talked about timestamp behavior before, I also cover it in my book, so I’ll just cover the basics.

Many times where you create or update a record in the database, you want to keep track of when you did it. This need is so common, that I’m almost tempted to push the behaviors method one layer down by creating my own class extending ActiveRecord and then by having all my models extend from that class. for example:

class MyActiveRecord extends ActiveRecord

 public function behaviors()
        return [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                'value' => new Expression('NOW()'),


So now, if I built all my models to extend MyActiveRecord instead of ActiveRecord, I get my created_at and updated_at fields updated automatically, assuming I have those fields in my DB table. Of course you would have to include a namespace and the appropriate use statements for it to work.

I generally tend not to use this approach, it’s too much for me because not every model that uses ActiveRecord I create will need it, and I prefer to do things the same way every time when I can, even if that is slightly more procedural. For example, I could extend MyActiveRecord when I want timestamp behavior and extend ActiveRecord when I don’t need the behavior. But then I have to keep track of it. Things like that really depend on the size of the project.

If I find there is a lot of code duplication with the behavior at the end of a project, I can optimize, and put the behavior in its own class as in the example. I would rather do it then because while I’m developing, it’s easier for me to get into the flow of the code by seeing the behavior in the same class as the model.

I’m not saying my way is a best practice, just sharing my particular method of doing it. At least now you are aware of some options and can decide for yourself.

Another pre-built behavior that yii 2 has made for you is the blameable behavior, which, when used in combination with the timestamp behavior, looks like this:

public function behaviors()
        return [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                'value' => new Expression('NOW()'),
            'blameable' => [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',

What blameable does is insert the current user id into the fields created_by and updated_by. This is a super-convient way of doing things. Every time a model gets created or updated, we know who to blame. Don’t you just love the name of the class?

This works out really well for large systems, where multiple users are doing admin and you need to keep track of who is doing what. You can also use this for frontend implementations, for example if you had a posts table and you wanted to use this method to keep track of the author. Alternatively, you could set that in the controller, but the behavior relieves you of having to write any additional code. This is extremely efficient, easy to implement, and easy to understand the benefit.

Now if I were writing a lazy tutorial, I would stop there. However, it’s worth noting that to extract the blameable username and display it, we need 4 additional methods on our model and the Yii 2 guide on behaviors does not mention them. Since this is not from the guide, please keep in mind that this is my way to do it, not necessarily official, and there may be a more concise way to do it that I’m not aware of.

However, until the docs are more complete, I had to come up with my own solution, which I’m pretty happy with at this point. I’m a big fan of these one line methods, which the Yii 2 guide does suggest using for relationships, so that’s how I approached this.

I simply created a get method to map the id field of user to the created_by field of the current model. Then in the next method, getCreateUserName(), we can access the username using the magic get syntax in a ternary statement:

public function getCreateUser()
    return $this->hasOne(User::className(), ['id' => 'created_by']);

       * @getCreateUserName

public function getCreateUserName() 
    return $this->createUser ? $this->createUser->username : '- no user -';

I put in a condition for – no user – even though the blameable behavior will fire everytime and update to the current user. It doesn’t hurt to code in the possibility that there is no id stored, and in such a case, we return the string ‘- no user -‘. It would help you troubleshoot at the least.

Of course we had to do the same methods for updated_by, so that required 2 more methods.

public function getUpdateUser()
   return $this->hasOne(User::className(), ['id' => 'updated_by']);
       * @getUpdateUserName

public function getUpdateUserName() 
    return $this->createUser ? $this->updateUser->username : '- no user -';

I also had to add the following to my attribute labels method on my model:

 'createUserName' => Yii::t('app', 'Created By'),
 'updateUserName' => Yii::t('app', 'Updated By'),

And once you have those labels in place, you can use them in a view like so:

 <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            ['attribute'=>'createUserName', 'format'=>'raw'],
            ['attribute'=>'updateUserName', 'format'=>'raw'],
    ]) ?>

Don’t worry about the rest of the example, that is my Faq model in the DetailView::widget. Ther relevant lines are:

 ['attribute'=>'createUserName', 'format'=>'raw'],
 ['attribute'=>'updateUserName', 'format'=>'raw'],

Those labels reference our get methods, using magic syntax, which means the get is implied, not explicit and you have to make the first word lower case. So now, instead of getting an id number displayed, we get the actual username. Cool stuff.

You can see that when we talk about implementing things, we have to jump around a bit. We went from behaviors on the model to calling a get method on the view, so we can return the username instead of user id.

Controllers can also implement the behaviors method, and this is incredibly useful for access control. I did a complete tutorial on RBAC using behaviors on the controllers, you should check that out if you want to continue on to see how you can use behaviors that way. I made implementing RBAC pretty simple, boiling it down to six easy steps.

I also cover this topic more extensively in my book, Yii 2 For Beginners.

Well, that’s it for this tutorial. Thanks for reading and thanks for your support. Feel free to add your comments and thoughts, it’s appreciated, as are links and reviews. Thanks again.

Yii 2 Dropdown List with Boolean Values

Sometimes it’s the simplest tasks that cause us the most grief. For example, a couple of days ago, I conceived of and wrote a tutorial for Super Simple RBAC in Yii 2 in about 20 minutes. What got me excited about that tutorial is that I reduced down the complexity of implementing RBAC, in other words granting special privileges and access to users with a role value of Admin, to six simple steps.

Of course that would not be possible if not for Yii 2. I know of no other framework that hands you so much power out-of-the-box that can help you move along so quickly. I’ve already gotten some great feedback on it and it seems like a nice solution for those who need admin users, but don’t want it to be too complex.

I cover this subject in my book, Yii 2 For Beginners in great detail, with a somewhat more robust solution with graphical UI for assigning roles. Also, a quick word of thanks to everyone who has purchased a copy, I appreciate your support.

Anyway, writing the RBAC tutorial had me feeling like a jr. Ninja and I was pretty happy about it. Unfortunately, I got splashed down to Earth on something really simple that took me a couple of hours to get right.

Have you ever had that frustrating feeling of knowing that what you want to do is so simple, and yet you just don’t know the right syntax to do it? So off you go, into experimentation mode to try to figure it out.

Now it would be nice if you could just look up everything in one awesome book, and the official guide is a great work in progress, but as of this writing, it is still incomplete. And even when it is complete, it is not going to cover every scenario.

It’s also not always clear what the best path to take is. For example, I’ve gotten quick at creating the little methods on the model that define relationships, and these are very useful for relationships. You can create dropdown lists with these methods in the views that really make your forms and view pages very intuitive for users.

But the ones I’m used to doing are based on relationships, which means they are built a certain way, involving other models and data tables. Now that sounds more complex, and it probably is, but like I said, I’ve done enough of them to be fairly comfortable with them at this point. I cover them extensively in my book.

But what if you just need to return a boolean value in a dropdown list that you want represented as a yes or a no? Does it make sense to put that in a DB table and create a relationship around it?

This is where the conversation gets interesting. You could do it that way, and if you were following a strict convention, doing it the same way as your other relationships, there’s some effeciency in that. You are always doing things the same way, therefore your code is easier to maintain and extend.

But here’s the catch. A boolean as a data structure is highly efficient, especially with MySql. That means if you get into complex querries that utilize the boolean value, it’s better to have the data type enforced as a boolean, MySql will perform better. It may only be a small difference, but small differences scaled over a large project can really add up.

Yii 2 Mobile First Design

One of the amazing things I love about Yii 2 is that it delivers a mobile-first, device scaling interface right out of the box. This is done via Twitter Bootstrap, which is the default implementation.

I never really knew how powerful this was until I worked on my template that I describe in my book, Yii 2 For Beginners.

I’ve never really had any talent for frontend design, and always felt that programmers were being punished for having too much fun coding by having to deal with frontend css, jquery, etc. Life would be too perfect if not for frontend development. I’m sure you can relate to that.

However, with the adoption of Twitter Bootstrap, that is starting to change. And maybe I should acknowledge just how awesome that frontend framework really is, since it does so much.

For example, you know those really cool top pins, where the top nav is pinned to the top of the page? That is out-of-the-box standard in Yii 2 courtesy of bootstrap. Another thing about it that is really cool is that when you size down the browser, it automatically detects the size of the browser and compensates. You don’t have to write a mobile version, it’s already there for you, how cool is that?

Plus Yii 2 has really cool bootstrap widgets designed to make it easy to integrate your view code with Bootstrap. I think you’ll be amazed at how much you get out-of-the-box.

I just did a quick tutorial on the dropdown nav and dropdown menu, so you can find that just by clicking the link. You’ll see how easy it is to implement.

In my book, I don’t focus too much on frontend, but we do look at some nice widgets, such as a pretty JUI DatePicker, Collapse, Alert, and more. It’s so simple when you see it in action.

Speaking of my book, please let me mention that I recently got my first book review from Vegibit:

“Another great resource is a great book by Bill Keck who maintains a blog about Yii. You can get the book at leanpub right here. It’s a great resource because it takes the perspective of someone who is new to the framework and is diving in with a self learning crash course in getting things working with Yii. This real world approach is valuable in learning the key concepts when the rubber hits the road so to speak.”

Thank you Vegibit for the kind words. When you get a chance, check them out, they are a good source for tutorials on Yii 2.

With the whole world moving toward mobile-first development, you can be confident that when you are developing on Yii 2, you are using a framework that gets it. The framework itself will provide most of what you need, right on install, and then after that, it’s easy to import extensions from Packagist.org.

I take a certain amount of pride in seeing the browser scale perfectly with my UI, even in reality, it wasn’t me, it was bootstrap. It’s just so cool!

If you would like more info on Bootstrap itself, just visit Get Bootstrap.

In a future tutorial, I’m going to write about how to configure your Yii 2 application to use a CDN for Bootstrap, jquery, and font-awesome, the dynamic trio of libraries that will make your UI sizzle. These libraries help take the drudgery out of frontend UI work and it makes development more fun, which is half the battle. Using a CDN will make them download faster and we all need that.

Feel free to add your comments to our blog and join the conversation. Thanks for your support!

Yii 2 RBAC Tutorial

Once again a warm welcome goes out to all the programmers and developers around the world who are discovering this blog. I get amazed every day when I see the stats and the diversity in the countries represented. It gives me the hope of a common language for the world and a framework like Yii 2 is a great place to express that.

Also, thanks to everyone who has taken part in the polls on this site. Over 1000 developers have voted on many topics relevant to today’s php community. Your comments are also appreciated, so please feel free to vote and comment.

And also, much gratitude for everyone who has purchased a copy of Yii 2 For Beginners, with your support I can continue my work, and I really appreciate it.

Ok, so the topic here is RBAC. This stands for Role Based Access Control and this can be difficult to implement in any framework. The Yii 2 advanced template comes with a working user model out of the box and this makes it fairly easy to get up and running. The thing is every site needs access control, especially if you are going to have a front end and back end to the site, and if you are going to have users with special privileges, such as admin users, who will have access to admin functions in the backend of the application.

The Yii 2 advanced template conveniently divides itself into frontend and backend out-of-the-box, so there again, it’s helping you. It stops short, however, of handing you a working RBAC model with the template. The reason for this is that many applications will have many different requirements and they need to leave the framework as open-ended as possible.

Yii 2 does have an authManager component with a complete RBAC implementation that is fully scalable and robust. You can read about RBAC in the docs. And if you want to use that, it’s perfectly fine. Personally, I found it to be a little rigid for my needs.

This is not saying anything bad about what they have, it’s just it’s impossible to anticipate every scenario, and its easier to build something to suit your own needs than follow someone else’s pattern. Plus there is a learning curve as there is with everything.

This is one of those instances where I really feel it’s better to write your own code, you will be closer to it, and it will be easier in the long run for you to work with it. Some may disagree with that statement, but like a lot of things in programming, it comes down to personal preference, so feel free to decide for yourself.

In my book, I layout an RBAC pattern that is easy for beginners to grasp and is something you can build on. But since it’s meant to be a little more robust for long-term use, it doesn’t lend itself to a quick tutorial.

At the same time, I noticed in the forum that there were a lot of people asking about how to implement RBAC. So I asked myself, is it quick and easy for someone to get up and running with RBAC if they just want user/admin roles? No. Wait… Yes it is!

I came up with a six step RBAC tutorial, which I’m linking to because I made a wiki tutorial out of it on the Yii Framework site.

You will need to have a fresh install of the Yii 2 advanced template. They have a complete set of setup instructions there. I cover it extensively in my book, with a bunch of jpgs to help you see what it is supposed to look like.

Anyway, for the tutorial, it’s a more basic version of what we cover in the book. I was able to bring it down to just six steps, it doesn’t require any outside plugin or extension, and you can do it in less than 20 minutes. I’m really happy about that.

It also uses Yii 2’s matchCallback method on behaviors, which I felt was good way to do it, since it leaves the main controller actions less cluttered. It’s really easy to implement.

Now RBAC is not the first subject you should jump into if you are just starting with Yii 2, and depending on your skill level, it can be difficult. The six step tutorial is about as simple as it gets, but you need to get the advanced template setup and working first, so make sure you have done that step before moving into RBAC.

On the other hand, I don’t know of any other php framework that can move you so far along, so quickly with RBAC. Yii 2 does 99.9% of the work, you have to love that.

Got my first book review from Vegibit:

“Another great resource is a great book by Bill Keck who maintains a blog about Yii. You can get the book at leanpub right here. It’s a great resource because it takes the perspective of someone who is new to the framework and is diving in with a self learning crash course in getting things working with Yii. This real world approach is valuable in learning the key concepts when the rubber hits the road so to speak.”

Feel free to add your comments below. We would love to hear from you. Thanks again for your support.

Yii 2 Dropdown Navigation

Yii 2 Dropdown Navigation and Menu tutorial

I would like to extend a warm welcome to all our friends from around the world who are just discovering this blog. Doing this blog, and seeing all the traffic from so many countries, it really does show me how much we all have in common, no matter where we are from. International bounderies are just an artificial construct, and not necessarily a good one. But as programmers and developers, we get to transcend all that and play in a very fun space.

Speaking of fun, 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.

You may have heard me say this before and most likely, you will hear me say it again:

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

Now anyone who knows me knows that I try to avoid frontend development as much as possible. It’s like God invented frontend development just to irritate backend developers, who otherwise just enjoy coding a little too much. But with Yii 2’s native integration of Bootstrap 3, a mobile responsive css out of the box, there’s no reason to feel that way.

Yii 2 has a number widgets that make integrating with Bootstrap easy. The tricky part is finding the documentation on them. I was following the forum and saw a lot of questions on Dropdown Navigation or a dropdown menu, as in, how do we do it? That’s a really basic question, everyone will need this at one point or another.

Now trying to be helpful, I searched for the answer and came up with nothing. I even dug into the source code of the widgets to see if I could figure it out. Somewhere along the way, I figured it out. Of course I have no way to know if this is the “official way” to do it, but I’ve tested it and it works. So here is the implementation:

Yii 2 Dropdown Navigation:

echo Nav::widget([
                'options' => ['class' => 'navbar-nav navbar-right'],
                'items' => [
                            ['label' => 'Top Label', 'items' => [
                            ['label' => 'Action', 'url' => '#'],
                            ['label' => 'Another action', 'url' => '#'],
                            ['label' => 'Something else here', 'url' => '#'],

Turns out it’s just a nested array inside the items array. So simple! I know they are working on the documentation and that Yii 2 has just been released, so we all just have to have patience as they put all the docs together. They are doing a good job of it, the doucmentation they have is clear, we just need more of it.

Another request I see often in the forum is for a working RBAC solution. I’ve developed a nice solution in Yii 2 For Beginners, it’s easy to implement, with a full backend UI, but it’s one of the more extended lessons in the book and it’s too big for a blog post.

Getting back to the Yii 2 DropDown Navigation widget, please remember you have to echo the widget inside the view. That means you have to place it within Php tags. Also, if you want to the dropdown navigation inside the existing Navbar widget that comes with the default Yii 2 installation, you need to place it somewhere within the widget begin and widget end tags:

                'brandLabel' => 'Yii 2 Build <i class="fa fa-plug"></i>',
                'brandUrl' => Yii::$app->homeUrl,
                'options' => [
                    'class' => 'navbar-inverse navbar-fixed-top',

//your nav widget somewhere inbetween.


Another tip is you can see I included a call to font-awesome’s fa fa-plug in the brandLabel parameter of the NavBar widget. Font-awesome is a great library and most likely, you will want to include in your project. It’s also a topic, we cover in the book.

Just keep in mind that if you come up against a situation like not knowing how to do a dropdown menu in Yii 2, the answer is out there and someone will help you. The Yii 2 community is very active and willing to help. It’s a great framework and I’m committed to helping you make the most of it.