On a little bit of YII2 database-related knowledge and a strong GII, this is the same as in the last article: to implement user registration and login in Yii2.
You can go directly to GitHub to download the source code, so that you can keep up with the progress, you can also start over, step by step follow this tutorial to do. This issue of user registration and login, I will use a great composer Package:dektrium/yii2-user, let's start our story here.
User's registration and login
In today's Web application, almost every application will require user registration, whether it is a third party or a self-built registration system, we need some forms to collect some necessary user data. These functions are not difficult to achieve in the YII2, and there are many ways, as if many methods are relatively straightforward, simple and rude. This may be the reason many people like Yii, just as many people like PHP, is simply rude!
In fact, in Yii2, it comes with a login implementation:
But we are going to make this wheel again because we need some more practical improvements, such as: When registering, send a verification email. This is almost something that every web app will consider when registering.
If you are installing the YII2 Advanced Application Template, then Yii2 is actually writing all of these features, and you will have a backend management module. But our tutorial is based on Yii2‘s Basic Application Template
, and I encourage you to build this wheel.
Take our M.O., we're going to build the wheels.
Installing Yii2-user
Our Yii2-User
installation steps here refer Yii2-User
to the official installation documentation. We use the composer directly to install:
require "dektrium/yii2-user:0.9.*@dev"
Wait a little bit, after the installation is ready for the corresponding configuration, we need to configure the file is config/web.php
, find components
, and then in the same position with its peers to add one modules
:
‘components‘ => [ // other settings... ],‘modules‘ => [ ‘user‘ => [ ‘class‘ => ‘dektrium\user\Module‘, ‘confirmWithin‘ => 21600, ‘cost‘ => 12, ‘admins‘ => [‘admin‘] ], ],
You'll also need to components
user
comment out the sections below, or you'll never be able to log in:
‘user‘ => [ ‘identityClass‘ => ‘app\models\User‘, ‘enableAutoLogin‘ => true, ],*/
The last step is to execute the Yii2-user migration
, which is helloYii/
executed under the directory:
php yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations
Then you will see:
Decisiveyes
Bang, here Yii2-user installation and configuration has been completed.
Configure Swiftmailer
After installing Yii2-user we don't have to worry about how to achieve login and registration (in fact, it is relatively simple), we said before the goal is to achieve the user registration time to send a verification email, here we first configure our mailbox service, Because Yii2-user can directly use the mailbox for registration verification and password retrieval and other functions. In config/web.php
finding mailer
this section:
‘mailer‘ => [ ‘class‘ => ‘yii\swiftmailer\Mailer‘, // send all mails to a file by default. You have to set // ‘useFileTransport‘ to false and configure a transport // for the mailer to send real emails. ‘useFileTransport‘ => true,],
Modify it to look like this:
' Mailer ' = [ ' class ' = ' Yii\swiftmailer\mailer ', ' viewPath ' = ' @app/mailer ', ' Usefiletransport ' and Span class= "Hljs-keyword" >false, ' transport ' = = [ ' class ' = > ' Swift_smtptransport ', ' host ' = ' username ' = [email protected] ', ' password ' + ' Your-password ', Span class= "hljs-string" > ' port ' = ' 587 ', ' encryption ' = ' TLS ',],],
Here because I often use is outlook
, do not think I am a wonderful. So I'm using the outlook
SMTP configuration here, you can make the corresponding changes according to your own needs.
Getting Started with Yii2-user
After the mailbox is configured, we can start using Yii2-user, first we'll modify our navigation bar, because what we want to achieve is the registration and login buttons that we often see on the right side of the navigation bar. When /views/layouts/main.php
found:
Echo Nav::widget ([' Options ' = [' Class ' =' Navbar-nav navbar-right '],' Items ' = [[' Label ' =' Home ',' URL ' = ['/site/index '], [' Label ' =' Status ',' Items ' = [[' Label ' =' View ',' URL ' = ['/status/index '], [' Label ' =' Create ',' URL ' = ['/status/create '],],], [' Label ' =' About ',' URL ' = [ ' label ' = Contact ', ' url ' = = [ '/site/contact ']], Yii:: $app->user->isguest? [ ' label ' = = ' Login ', ' url ' = [ '/site/login '] : [ ' label ' = ' Logout ('. Yii:: $app->user->identity->username. ' url ' = = [ ' linkoptions ' = [ ' data-method ' = = ' post ']], [],]);
The above revelation is the code of the navigation bar that we modified in the previous article, and then replace it with the following code:
$navItems =[[' Label ' =' Home ',' URL ' = ['/site/index '], [' Label ' =' Status ',' URL ' = ['/status/index '], [' Label ' =' About ',' URL ' = ['/site/about '], [' Label ' =' Contact ',' URL ' = ['/site/contact '];if (Yii::$app->user->isguest) {Array_push ($navItems, [' Label ' =' Sign in ',' URL ' = ['/user/login ']],[' Label ' =' Sign up ', ' url ' = = [ '/user/register ']]); } else {array_push ( $navItems, [ Label ' = = $app->user->identity->username. ' url ' = = [ ' linkoptions ' = [ ' data-method ' = = ' post '] ); }echo nav::widget ([ ' options ' = [ ' class ' + ' Navbar-nav Navbar-right '], ' items ' = $navItems,]);
After the modification is complete, we directly access: http://localhost:8999/user/register
, you will see the following similar page:
Did you find it magical? Yes, Yii2-user, help us all write! Then we enter the appropriate information click Register, then you will see this information prompt page:
Prompt new said that the verification mailbox has been sent, we login QQ mailbox to see, sure enough:
See this, I believe everyone will be very happy, there is a picture of the truth. Click on the verification link for the email, and you'll see Yii2-user give us feedback on the verification success:
Notice the upper right corner, this time we have logged in to the app, if you click Logout will return to the login page:
Here, register login The entire process is complete, but there is a situation we often encounter in daily development: forget password. Well, for this situation, Yii2-user directly provides this function! You can access it directly: you http://localhost:8999/user/forgot
can see:
Well, that's it, it's easy. With Yii2-user This powerful composer package
, we can easily achieve user registration, login and forget password and other functions. Of course, Yii2-user has a lot of features, and we just use a very small part of it, and you can see it directly in the documentation:
Https://github.com/dektrium/yii2-user/blob/master/docs/README.md
Finally, I hope this article can help you solve some problems. Next I will be able to talk about user rights control and management, because the implementation of the user registration, so the next article appears to be natural.
The source will be placed in Github:https://github.com/jellybool/helloyii
YII2 Series Tutorial Four: Implement user registration, authentication, login