YII2 Basic version login using Yii:: $app->user->login ($user); error occurred

Source: Internet
Author: User
Tags array to string findone yii

Yii2 basicVersion of login using Yii::$app->user->login($user); errorArray to string conversion

    1. 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 ']); }}
    1. 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; }}
    1. 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 } ?-->
    1. 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 basicVersion of login using Yii::$app->user->login($user); errorArray to string conversion

    1. 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 ']); }}
    1. 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; }}
    1. 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 } ?-->
    1. 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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.