传递给yii \ web \ User :: login()的参数1必须实现接口yii \ web \ IdentityInterfac,null给出

时间:2015-08-24 21:16:46

标签: api rest login yii2

我正在尝试创建一个登录API并且已经跟随链接https://github.com/yiisoft/yii2/blob/master/docs/guide/security-authentication.md

上面的网址说:

  

要登录用户,您可以使用以下代码:

// find a user identity with the specified username.
// note that you may want to check the password if needed
$identity = User::findOne(['username' => $username]);

// logs in the user 
Yii::$app->user->login($identity);
     

应该在控制器操作或实现IdentityInterface的类中添加此行。

在控制器操作中添加上面的行,但我得到错误说 Argument 1 passed to yii\web\User::login() must implement interface yii\web\IdentityInterface, null given, called in C:\wamp\www\basic\controllers\MyController.php on line 202并定义 我实现Identity接口的类实际上是一个名为Myuser的模型(app \ models \ myuser)。

为什么说yii\web\User::login()必须实现身份界面?

如果要在Myuser类中添加它,如何将发布的参数传递给登录函数?

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

您的User模型必须实现IdentityInterface

class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface{
    ...
}

答案 1 :(得分:0)

Yii::$app->getUser()->login($user);  
var_dump($user); 

试试吧,你可以得到答案。

我遇到了同样的问题,并解决了。 var_dump($user),您可以知道

答案 2 :(得分:0)

你可以实现多个接口,

User extends \yii\db\ActiveRecord implements IdentityInterface, OtherInterface 
{
... 
}