Php 如何在没有数据库的情况下登录Yii2?

Php 如何在没有数据库的情况下登录Yii2?,php,authentication,yii2,Php,Authentication,Yii2,我需要帮助 我有一个使用DB登录的工作机制,但有时我需要在没有DB的情况下获得登录过程(假用户使用) 用户模型中的静态方法 public static function findByRoot() { $arr = [ 'id' => 100, 'created_at' => 1444322024, 'updated_at' => 1444322024, 'username' => 'vasya', 'aut

我需要帮助

我有一个使用DB登录的工作机制,但有时我需要在没有DB的情况下获得登录过程(假用户使用)

用户模型中的静态方法

public static function findByRoot()
{
   $arr = [
      'id' => 100,
      'created_at' => 1444322024,
      'updated_at' => 1444322024,
      'username' => 'vasya',
      'auth_key' => 'aagsdghfgukfyrtweri',
      'password_hash' => 'aa2gsdg123hfgukfyrtweri',
      'email' => 'some@email',
      'status' => 10,
    ];
    return new static($arr);
}
我也尝试过其他变量方法,如:

public static function findByRoot()
  {
    $model = new User();
    $model->id = '1000';
    $model->username = 'vasya';
    $model->status = 10;
    return $model;
  }
Yii::$app->getUser()->login()
需要来自UserIdentity的工具

进行授权:

\Yii::$app->getUser()->login(User::findByRoot());
如果我在
login
方法中输入db中的实名,它返回
TRUE
,这没关系

但是如果放置
User::findByRoot()
(同一个对象),它也返回
TRUE
但是
Yii::$app->User->identity
NULL


有什么问题吗?

Yii::$app->user->identity
在找不到用户id的情况下返回
null
。要解决这个问题,首先确保您在此处提供了正确的id:

public static function findIdentity($id)
{
    // dump $id here somehow, does it belong to the static collection?
    return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
第二个选择是,始终返回带有填充数据的实例,因为无论如何都要使用假数据来测试它

public static function findIdentity($id)
{
    // just ignore the $id param here
    return new static(array(
        'updated_at' => '...',
        'username' => '....',
        // and the rest 
    ));
}