Yii2 basic
Version of login using Yii::$app->user->login($user);
errorArray to string conversion
MyAuthenticationController.php
Request->post (' username ', null); $password = Yii:: $app->request->post (' password ', null); $user = User::findone ([' username ' = ' = $username]); if ($username!=null) && ($password!=null)) {if ($user!=null) {if ($user->validate Password ($password)) {$this->redirect ([' Index ']);//Here you can implement redirects//Yii:: $app->user ->login ($user); This is the source code, but there is an "Array to string conversion" error, not yet resolved, so use the redirection code above to the specified login successful page}else{ $error = ' Password validation failed '; }}else{$error = ' User not found '; }} return $this->render (' login ', [' ERROR ' = $error]); } public Function Actionlogout () {Yii:: $app->user->logout (); return $this->redirect ([' Login ']); }}
app\models\User.php
$ID]); }/** * @inheritdoc */public static function Findidentitybyaccesstoken ($token, $type = null) {RE Turn Static::findone ([' Access_token ' + $token]); }/** * Finds user by Username * * @param string $username * @return Static|null */Public Stati C function Findbyusername ($username) {return static::findone ([' username ' = $username]); }/** * @inheritdoc */Public Function getId () {return $this->primarykey (); }/** * @inheritdoc * * * Public Function Getauthkey () {return $this->auth_key; }/** * @inheritdoc */Public Function Validateauthkey ($authKey) {return $this->getauthkey () = = = $authKey; }/** * Validates password * * @param string $password password to validate * @return Boolean if passwo RD provided is valid for current user */Public Function ValidatePassword ($password) {return YiI:: $app->security->validatepassword ($password, $this->password_hash); Public Function Generateauthkey () {$this->auth_key = Yii:: $app->security->generaterandomkey (); The Public Function BeforeSave ($insert) {if (Parent::beforesave ($insert)) {if ($this->isnewrecor d) {$this->auth_key = \yii:: $app->security->generaterandomstring (); } return true; } return false; }}
-
views\my-authentication\login.php
!--? phpuse \yii\bootstrap\activeform;use \yii\helpers\html;use \yii\bootstrap\alert,?-->!--? phpif ($ Error!=null) {echo alert::widget ([' Options ' =-->[' class ' = ' Alert-danger '], ' body ' = $error]); >!--? php if (Yii: $app--->user->isguest) {?> "!--? php activeform::begin ()?--"!--? php Echo Html::label (' Username ', ' Username ');?-->!--? php echo html::textinput (' Username ', ' ', [' Class ' =--> ' Form-control '] );? >!--? php echo Html::label (' Password ', ' Password '),?-->!--? php echo Html::p asswordinput (' p Assword ', ', [' Class ' =--> ' Form-control ']);?>!--? php echo Html::submitbutton (' Login ', [' Class ' =--> ' btn Btn-pri Mary ']);?>!--? php activeform::end (),?-->!--? php} else {?--> are authentication!
!--? php echo html::a (' logout ', [' My-authentication/logout '],[' class ' =--> ' btn btn-warning ']);?>!--? php } ?-->
Sql
---- 表的结构 `user`--CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `auth_key` varchar(32) NOT NULL, `password_hash` varchar(255) NOT NULL, `access_token` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;---- 转存表中的数据 `user`--INSERT INTO `user` (`id`, `username`, `auth_key`, `password_hash`, `access_token`) VALUES(1, 'foo', '', '$2a$12$hL0rmIMjxhLqI.xr7jD1FugNWEgZNh62HuJj5.y34XBUfBWB4cppW', NULL);
The user name is foo
, the password is foopassword
, after logging in, an Yii::$app->user->login($user);
error occurs,
Reply content:
Yii2 basic
Version of login using Yii::$app->user->login($user);
errorArray to string conversion
MyAuthenticationController.php
Request->post (' username ', null); $password = Yii:: $app->request->post (' password ', null); $user = User::findone ([' username ' = ' = $username]); if ($username!=null) && ($password!=null)) {if ($user!=null) {if ($user->validate Password ($password)) {$this->redirect ([' Index ']);//Here you can implement redirects//Yii:: $app->user ->login ($user); This is the source code, but there is an "Array to string conversion" error, not yet resolved, so use the redirection code above to the specified login successful page}else{ $error = ' Password validation failed '; }}else{$error = ' User not found '; }} return $this->render (' login ', [' ERROR ' = $error]); } public Function Actionlogout () {Yii:: $app->user->logout (); return $this->redirect ([' Login ']); }}
app\models\User.php
$ID]); }/** * @inheritdoc */public static function Findidentitybyaccesstoken ($token, $type = null) {RE Turn Static::findone ([' Access_token ' + $token]); }/** * Finds user by Username * * @param string $username * @return Static|null */Public Stati C function Findbyusername ($username) {return static::findone ([' username ' = $username]); }/** * @inheritdoc */Public Function getId () {return $this->primarykey (); }/** * @inheritdoc * * * Public Function Getauthkey () {return $this->auth_key; }/** * @inheritdoc */Public Function Validateauthkey ($authKey) {return $this->getauthkey () = = = $authKey; }/** * Validates password * * @param string $password password to validate * @return Boolean if passwo RD provided is valid for current user */Public Function ValidatePassword ($password) {return YiI:: $app->security->validatepassword ($password, $this->password_hash); Public Function Generateauthkey () {$this->auth_key = Yii:: $app->security->generaterandomkey (); The Public Function BeforeSave ($insert) {if (Parent::beforesave ($insert)) {if ($this->isnewrecor d) {$this->auth_key = \yii:: $app->security->generaterandomstring (); } return true; } return false; }}
-
views\my-authentication\login.php
!--? phpuse \yii\bootstrap\activeform;use \yii\helpers\html;use \yii\bootstrap\alert,?-->!--? phpif ($ Error!=null) {echo alert::widget ([' Options ' =-->[' class ' = ' Alert-danger '], ' body ' = $error]); >!--? php if (Yii: $app--->user->isguest) {?> "!--? php activeform::begin ()?--"!--? php Echo Html::label (' Username ', ' Username ');?-->!--? php echo html::textinput (' Username ', ' ', [' Class ' =--> ' Form-control '] );? >!--? php echo Html::label (' Password ', ' Password '),?-->!--? php echo Html::p asswordinput (' p Assword ', ', [' Class ' =--> ' Form-control ']);?>!--? php echo Html::submitbutton (' Login ', [' Class ' =--> ' btn Btn-pri Mary ']);?>!--? php activeform::end (),?-->!--? php} else {?--> are authentication!
!--? php echo html::a (' logout ', [' My-authentication/logout '],[' class ' =--> ' btn btn-warning ']);?>!--? php } ?-->
Sql
---- 表的结构 `user`--CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `auth_key` varchar(32) NOT NULL, `password_hash` varchar(255) NOT NULL, `access_token` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;---- 转存表中的数据 `user`--INSERT INTO `user` (`id`, `username`, `auth_key`, `password_hash`, `access_token`) VALUES(1, 'foo', '', '$2a$12$hL0rmIMjxhLqI.xr7jD1FugNWEgZNh62HuJj5.y34XBUfBWB4cppW', NULL);
The user name is foo
, the password is foopassword
, after logging in, an Yii::$app->user->login($user);
error occurs,
public function getId(){ return $this->getPrimaryKey();}
NoPrimaryKey