Yii Framework 2.0用户数据库登录

编程入门 行业动态 更新时间:2024-10-27 20:35:20
本文介绍了Yii Framework 2.0用户数据库登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在试图在互联网上搜索如何编写Yii框架2.0中的代码,以便用户可以使用存储在数据库中的凭据登录,而不是从数组,前缀在models / User.php。我知道如何做在Yii 1。但在Yii 2.0,我真的不知道如何这样做。因为Yii 2.0还没有发布(只有beta版本可用),我没有找到许多Yii 2.0教程在Internet上关于登录数据库。请帮我这个!非常感谢!

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. Please help me with this! Thanks in advanced!

推荐答案

您可以使用 github/amnah/yii2-user 。

如果您想编写自己的自定义脚本来管理用户,您可以覆盖Yii2 identityClass。

If you want to write your own custom script to manage the users you can override Yii2 identityClass.

您的配置添加:

'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, ],

请注意您的用户模型MUST IMPLEMENT \yii \ web \IdentityInterface

Please note that your user model MUST IMPLEMENT \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 :)

更多推荐

Yii Framework 2.0用户数据库登录

本文发布于:2023-10-30 03:16:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   用户   Yii   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!