php Yii Framework 2.0 使用用户数据库登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25790543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii Framework 2.0 Login With User Database
提问by O Connor
I have been trying to search on the Internet how to write the code in Yii framework 2.0 so that user can login with the credentials stored in the database and not from the array, prefixed in models/User.php. I know how to do that in Yii 1. But in Yii 2.0, I really don't know how to do that. Since Yii 2.0 has not been released yet (only the beta version is available), I could not find many Yii 2.0 tutorials on the Internet about logging-in with the database.
我一直试图在互联网上搜索如何在 Yii 框架 2.0 中编写代码,以便用户可以使用存储在数据库中的凭据而不是从数组中登录,前缀为模型/User.php。我知道如何在 Yii 1 中做到这一点。但在 Yii 2.0 中,我真的不知道如何做到这一点。由于 Yii 2.0 还没有发布(只有 beta 版),我在网上找不到很多关于使用数据库登录的 Yii 2.0 教程。
回答by Rx Seven
You can implement database user management using extesions like https://github.com/amnah/yii2-user.
您可以使用extesions像实现数据库用户管理https://github.com/amnah/yii2-user。
OR
或者
If you want to write your own custom script to manage the users you can override Yii2 identityClass.
如果您想编写自己的自定义脚本来管理用户,您可以覆盖 Yii2 identityClass。
In the component section of your config add:
在配置的组件部分添加:
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
Please note that your user model MUST IMPLEMENT \yii\web\IdentityInterface
请注意,您的用户模型必须实现 \yii\web\IdentityInterface
Here is the example of the model class that you can use to implement database authentication
以下是可用于实现数据库身份验证的模型类示例
namespace app\models;
//app\models\gii\Users is the model generated using Gii from users table
use app\models\gii\Users as DbUser;
class User extends \yii\base\Object implements \yii\web\IdentityInterface {
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
public $email;
public $phone_number;
public $user_type;
/**
* @inheritdoc
*/
public static function findIdentity($id) {
$dbUser = DbUser::find()
->where([
"id" => $id
])
->one();
if (!count($dbUser)) {
return null;
}
return new static($dbUser);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $userType = null) {
$dbUser = DbUser::find()
->where(["accessToken" => $token])
->one();
if (!count($dbUser)) {
return null;
}
return new static($dbUser);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username) {
$dbUser = DbUser::find()
->where([
"username" => $username
])
->one();
if (!count($dbUser)) {
return null;
}
return new static($dbUser);
}
/**
* @inheritdoc
*/
public function getId() {
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey() {
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey) {
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password) {
return $this->password === $password;
}
}
I hope that would be helpful to you . Cheers :)
我希望这对你有帮助。干杯:)
回答by user1852788
Check the Yii2 advanced application:
查看 Yii2 高级应用程序:
https://github.com/yiisoft/yii2-app-advanced
https://github.com/yiisoft/yii2-app-advanced
There full implementation of signup, login, resetPassword for Users stored in DB.
数据库中存储的用户的注册、登录、重置密码的完整实现。