In this tutorial I’m going to explain how to implement login from database functionality in Yii Framework 2. I’ve seen a lot of people having problems with implementing login functionality, and with understanding the concept.

There are two classes and one interface you have to understand in order to implement login functionality in Yii Framework 2. First is a yii/web/User class which is a user component available in yii/web/Application object, the second one is your custom class which is an object which represents a database table called user, admin_user, backend_user, administrator or any other table in database which represents your website user. Your custom table should have at least the following columns: username and password where username is unique identifier of your user. Do not mix yii/web/User class which is a default Yii framework component with your custom class which can also be named User (in video I intentionally name my custom class BackendUser).

In my example, in MySQL database I created a backend_user table with the following columns:

Column name Column description
id User id
firstName First name of a user
lastName Last name of a user
username Username (unique)
password Password
authKey Auth key column required for cookie based login. Should be unique for every user

Your custom class should implement an interface called IdentityInterface which defines several methods. In my example, BackendUser (my custom class which I generated using Gii tool) is implementing IdentityInterface interface, as displayed bellow:

In video I explained why I added additional two functions called validatePassword() and findByUsername().

Now when you implemented your custom class (in my example called BackendUser), you have to update your web.php configuration file. In your components section, copy and paste the following code:

As you can see, property identityClass which is defined in yii/web/User class is pointing to my custom class BackendUser which implements yii/web/IndentityInterface. If you don’t have authKey column in your database then set enableAutoLogin property to false and in your custom class (in my example BackendUser) getAuthKey and validateAuthKey like:

If you don’t do it like that, you will get the following exception:

Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: app\models\User::auth_key

This way you can implement login functionality from file, Active Directory (AD), database, web service or any other resource.

I’d love to get your comments below or you can email me at code.epicenter at gmail.com.

How to login user from a database in Yii Framework 2http://code-epicenter.com/wp-content/uploads/2015/12/Yii2-How-to-login-user-from-a-database-Video-tutorial-1024x632.pnghttp://code-epicenter.com/wp-content/uploads/2015/12/Yii2-How-to-login-user-from-a-database-Video-tutorial-150x150.pngAmir DuranPHPProgrammingTutorialsVideo TutorialsYii 2tutorial,video tutorial,Yii framework,Yii Framework 2
In this tutorial I'm going to explain how to implement login from database functionality in Yii Framework 2. I've seen a lot of people having problems with implementing login functionality, and with understanding the concept. There are two classes and one interface you have to understand in order to implement...