In this post I’m going to explain how to create different login screens if you are working with multiple modules in Yii framework. Please read my post about How to work with Yii framework modules.

Today most of the websites have two parts:

Backend – which is used for administration purposes (creating new posts, adding new products etc…). Usually, this part of your website is available only to specific users usually called administrators or content managers.

Frontend – The part of the website which is available to normal users or customers

So let say this is your case and you want to create different login screens for backend and frontend part of the website.

Backend module

First step is to set default login action for every module. You can do it in your module class. In our case, backend module class will be generated in protected/modules/backend/Backend.php. In init() function of your model class add this piece of code:

Here we define a new module component called user (you can change the name of the component as I will do in frontend module), which is defined by CWebUser  class (you can extend this class and put your class name here).

We have defined baseUrl property of the CWebUser class. We also defined stateKeyPrefix, which is the prefix that will be added to every variable in the cookie.

With this code, you configured your modul’s default login action and state key prefix which is first step of login process. Every time when user requests a page in backend, and if user is not logged in, user will be redirected to http://website.com/backend/user/login which is exactly what we wanted.

To access user component somewhere in the code, you can write:

Front end module

No the same thing you should do for the frontend module.

Here we defined a new module component called frontendUser (I intentionally changed the component name to frontendUser so it is not user again), which is defined by CWebUser  class. Now if you want to access this component, you can write the following code:

Implement login for different user types

Now, Yii will take care for default login URL’s if you are running backend or frontend module. Next step is to implement user/login action. Here are the steps that I usually take:

  • I create User controller where I implement login action. Create this controller inside both frontend and backend module. The body of the Login action you can copy from the Yii’s default Site controller (protected/controllers/SiteController.php)
  • In database I create separate table for Administrators and normal Customers (Users).
  • Extend UserIdentity class for Administrator database table and save it in your protected/modules/backend/components/ folder
  • Extend UserIdentity class for User database table and save it under protected/modules/frontend/components/ folder

Here is example how to properly extend Yii’s UserIdentity class so it works with your database table.

So we defined AdminIdentity class which extends UserIdentity class which submits Administrator database table for usernames.

Similarly, you can do for User table:

Now, you can use classes above in your LoginForm to properly login user.

 

How to create different logins for different modules in Yii frameworkAmir DuranPHPProgrammingTutorialsUncategorizedModules,PHP,Yii framework
In this post I'm going to explain how to create different login screens if you are working with multiple modules in Yii framework. Please read my post about How to work with Yii framework modules. Today most of the websites have two parts: Backend - which is used for administration purposes (creating new...