侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

yii怎样做登录

框架(架构)  /  管理员 发布于 7年前   207

yii做登录的方法示例:

1、LoginForm.php

用户登陆模块,所提交的是username和password,所以我们要先建立一个Model,专门处理用户提交的数据,所以先新建一个LoginForm.php,以下为代码:

<?php namespace app\modules\backend\models; use Yii;use yii\base\Model; /** * LoginForm is the model behind the login form. */class LoginForm extends Model{    public $username;    public $password;    public $rememberMe = true;     private $_user = false;      /**     * @return array the validation rules.     */    public function rules()<span style="white-space:pre"></span>//①    {        return [// username and password are both required[['username', 'password'], 'required','message'=>""],// rememberMe must be a boolean value['rememberMe', 'boolean'],// password is validated by validatePassword()['password', 'validatePassword'],        ];    }     /**     * Validates the password.     * This method serves as the inline validation for password.     *     * @param string $attribute the attribute currently being validated     * @param array $params the additional name-value pairs given in the rule     */    public function validatePassword($attribute, $params)    {        if (!$this->hasErrors()) {$user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) {    $this->addError($attribute, 'Incorrect username or password.');}        }    }     /**     * Logs in a user using the provided username and password.     * @return boolean whether the user is logged in successfully     */    public function login()    {        if ($this->validate()) {if($this->rememberMe){    $this->_user->generateAuthKey();//③}return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);        }        return false;    }     /**     * Finds user by [[username]]     *     * @return User|null     */    public function getUser()    {        if ($this->_user === false) {$this->_user = User::findByUsername($this->username); //②        }         return $this->_user;    }}

该Model是根据basic模板自带的LoginForm修改而成,代码中大多有注释,这里关注以下代码:

①号代码处是rules规则,rules规则定义了填充过来的数据的规则,验证所填的数据是否为空,是否符合格式之类的,其中有一栏是password,对应的规则是validatePassword,会自动调用当前类的validatePassword()方法,注意与下文的User类对应的方法区分。

②号代码,调用了User类里面的findByUsername方法,这个User类下面会写到,主要是为了返回一个AR类实例,与当前LoginForm的数据进行比较。

③号代码,这里暂时不提,等讲到cookie登陆的时候再提。

2、User.php

(1)ActiveRecord 类

在完成LoginForm后,我们还缺少一些东西,从用户接受到数据了,那么还需要从数据库取出相应的数据来进行比较,所以我们接下来需要完成的是一个从数据库获取的数据的类——AR类,全称是ActiveRecord,活动记录类,方便用于查找数据,只要类名和数据表的表名相同,那么它就能从这个数据表中获取数据,比如说这样:

<?phpnamespace app\modules\backend\models;use yii\db\ActiveRecord; class User extends ActiveRecord{       } ?>

还能自己添加返回的表名,只要在这个类中重写以下方法:

public static function tableName(){return 'user';}

(2)IdentityInterface 接口

一般来说,从数据库查找数据,只需要继承AR类即可,但是,我们这个是用户登录模型,核心是验证,所以自然需要实现核心的验证功能,就像LoginForm模型提到的validatePassword一样,实际的验证逻辑是在当前的User模型完成的。一般来说,实现IdentityInterface接口,需要实现以下方法:

    public static function findIdentity($id);  //①     public static function findIdentityByAccessToken($token, $type = null);   //②     public function getId();    //③     public function getAuthKey();   //④     public function validateAuthKey($authKey);    //⑤

①findIdentity:是根据id查找数据表对应的数据

②findIdentityByAccessToken是根据AccessToken(上文提到的)查找对应的数据,而AccessToken我们在数据表也有这个字段,那么它到底有什么用呢?其实AccessToken在我们当前的用户登陆模型中用处并不大,它是专门用于Resetful登陆验证用到的,具体可自行百度,这里不展开说明。

③getId:返回当前AR类所对应的id

④getAuthKey:返回当前AR类所对应的auth_key

⑤validateAuthKey:这个方法比较重要,是我们后面要讲到的cookie登陆验证的核心所在。

在我们的User.php实现接口,然后重写以上方法,完整的User.php的代码如下:

<?phpnamespace app\modules\backend\models;use yii\db\ActiveRecord; class User extends ActiveRecord implements \yii\web\IdentityInterface{ public static function tableName(){return 'user';} public static function findIdentity($id){return static::findOne($id);} public static function findIdentityByAccessToken($token,$type=null){return static::findOne(['accessToken'=>$token]);} public static function findByUsername($username){     //①return static::findOne(['username'=>$username]); } public function getId(){return $this->id;} public function getAuthkey(){return $this->auth_key;} public function validateAuthKey($authKey){return $this->auth_key === $authKey;} public function validatePassword($password){          //②return $this->password === md5($password);}    <span style="white-space:pre"></span> /**    <span style="white-space:pre"></span> * Generates "remember me" authentication key    <span style="white-space:pre"></span> */        public function generateAuthKey()        //③        {       <span style="white-space:pre"></span>$this->auth_key = \Yii::$app->security->generateRandomString();       <span style="white-space:pre"></span>$this->save();        } }?>

①findByUsername():在LoginForm的代码中,引用了这个方法,目的是根据用户提交的username返回一个在数据表与username相同的数据项,即AR实例。

②validatePassword():这里对用户提交的密码以及当前AR类的密码进行比较。

③generateAuthKey():生成随机的auth_key,用于cookie登陆。

一共写了两个Model类:LoginForm和User,一个用于接收用户提交的数据,一个用于获取数据库的数据。

控制器(Controller)

控制器,主要是用于数据的提交,把用户提交的数据填充到相应的模型(Model)中,然后根据模型返回的信息进一步渲染视图(View),或者执行其他逻辑。

这里,把控制器命名为LoginController.php,以下是完整的实现代码:

<?php namespace app\controllers; use Yii;use yii\filters\AccessControl;use yii\web\Controller;use yii\filters\VerbFilter;use app\models\LoginForm;use app\models\ContactForm; class SiteController extends Controller{    public function actionIndex()    {        return $this->render('index');    }     public function actionLogin()    {        if (!\Yii::$app->user->isGuest) {     //①return $this->goHome();        }         $model = new LoginForm(); //②        if ($model->load(Yii::$app->request->post()) && $model->login()) {      //③return $this->goBack();          //④        }        return $this->render('login', [      //⑤'model' => $model,        ]);    }     public function actionLogout()    {        Yii::$app->user->logout();         return $this->goHome();    }}

①首先从\Yii::$app->user->isGuest中判断,当前是否是游客模式,即未登陆状态,如果用户已经登陆,会在user类中储存当前登陆用户的信息。

②如果当前是游客,会先实例化一个LoginForm模型

③这行代码是整个login方法的核心所在,首先:$model->load(Yii::$app->request->post())把post过来的数据填充进$model,即LoginForm模型,如果返回true,则填充成功。接着:$model->login():执行LoginForm类里面的login()方法,可以从login()方法里面看到,将会执行一系列的验证。

视图(View)

在实现了model和controller,接下来是视图部分,由于用户需要输入数据,所以我们要提供一个表单,在Yii2中,提供了ActiveForm快速生成表单,代码如下:

<?php /* @var $this yii\web\View *//* @var $form yii\bootstrap\ActiveForm *//* @var $model app\models\LoginForm */ use yii\helpers\Html;use yii\bootstrap\ActiveForm; $this->title = 'Login';$this->params['breadcrumbs'][] = $this->title;?><div class="site-login">    <h1><?= Html::encode($this->title) ?></h1>    <p>Please fill out the following fields to login:</p>    <?php $form = ActiveForm::begin([        'id' => 'login-form',        'options' => ['class' => 'form-horizontal'],        'fieldConfig' => ['template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",'labelOptions' => ['class' => 'col-lg-1 control-label'],        ],    ]); ?>        <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>        <?= $form->field($model, 'password')->passwordInput() ?>        <?= $form->field($model, 'rememberMe')->checkbox(['template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",        ]) ?>        <div class="form-group"><div class="col-lg-offset-1 col-lg-11">    <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?></div>        </div>    <?php ActiveForm::end(); ?>    <div class="col-lg-offset-1" style="color:#999;">        You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>        To modify the username/password, please check out the code <code>app\models\User::$users</code>.    </div></div>

推荐学习:yii框架

以上就是yii怎样做登录的详细内容,更多请关注其它相关文章!


  • 上一条:
    yii怎么退出登录
    下一条:
    yii 英文读什么?
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客