Activecontroller And Modelclass

Good day.

I created a active controller for the rest api.

Set for modelClass my model:




class ApiController extends ActiveController

{

	public $modelClass = 'app/models/Devices';

...

public function behaviors()

	{

	    return ArrayHelper::merge(parent::behaviors(), [

	        'authenticator' => [

	            'class' => QueryParamAuth::className(),

	            'only' => ['find','activate'],

	      ],

...



In my model:




class Devices extends ActiveRecord implements \yii\db\ActiveRecordInterface

{

...

...

public static function findIdentityByAccessToken($token)

{

    return static::findOne(['token' => $token]);

}



but yii search findIdentityByAccessToken methon from User model.




{"type":"yii\\base\\NotSupportedException",

"name":"Not Supported",

"message":"\"findIdentityByAccessToken\" is not implemented.",

"code":0,

"file":"\/home\/projects\/pr1\/common\/models\/User.php",

"line":76,

"stack-trace":["#0 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/web\/User.php(255): common\\models\\User::findIdentityByAccessToken('OfIpDuopndjvA2n...', 'yii\\filters\\aut...')",

"#1 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/filters\/auth\/QueryParamAuth.php(34)



How can I fix this problem?

Thanks


class Devices extends ActiveRecord implements \yii\db\ActiveRecordInterface

Why are you mplementing ActiveRecordInterface? If it was a user class somehow you should implement Identity interface

from above linked example:


class User extends ActiveRecord implements IdentityInterface

{

    public static function findIdentity($id)

    {

        return static::findOne($id);

    }


    public static function findIdentityByAccessToken($token)

    {

        return static::findOne(['access_token' => $token]);

    }


    public function getId()

    {

        return $this->id;

    }


    public function getAuthKey()

    {

        return $this->authKey;

    }


    public function validateAuthKey($authKey)

    {

        return $this->authKey === $authKey;

    }

}

Hi.

Thank you for reply Stefano Mtangoo.

So, too, is still the same.

First I tried IdentityInterface, it doesn’t work, then read somewhere about ActiveRecordInterface and tried it too.

you dont try it blindly. What do you want to accomplish?

whats the meaning of " it doesn’t work"? It can mean a lot of different things!

Now, change implement interface to IdentityInterface:


 class Devices extends ActiveRecord implements IdentityInterface

{

...



still I’m getting this error:




{"type":"yii\\base\\NotSupportedException",

"name":"Not Supported",

"message":"\"findIdentityByAccessToken\" is not implemented.",

"code":0,

"file":"\/home\/projects\/pr1\/common\/models\/User.php",

"line":76,

"stack-trace":["#0 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/web\/User.php(255): common\\models\\User::findIdentityByAccessToken('OfIpDuopndjvA2n...', 'yii\\filters\\aut...')",

"#1 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/filters\/auth\/QueryParamAuth.php(34): yii\\web\\User->loginByAccessToken('OfIpDuopndjvA2n...', 'yii\\filters\\aut...')",

"#2 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/filters\/auth\/AuthMethod.php(48): yii\\filters\\auth\\QueryParamAuth->authenticate(Object(yii\\web\\User), Object(yii\\web\\Request), Object(yii\\web\\Response))",

"#3 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/base\/ActionFilter.php(71): yii\\filters\\auth\\AuthMethod->beforeAction(Object(yii\\base\\InlineAction))",

"#4 [internal function]: yii\\base\\ActionFilter->beforeFilter(Object(yii\\base\\ActionEvent))",

"#5 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/base\/Component.php(538): call_user_func(Array, Object(yii\\base\\ActionEvent))",

"#6 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/base\/Controller.php(257): yii\\base\\Component->trigger('beforeAction', Object(yii\\base\\ActionEvent))",

"#7 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/web\/Controller.php(108): yii\\base\\Controller->beforeAction(Object(yii\\base\\InlineAction))",

"#8 \/home\/projects\/pr1\/vendor\/yiisoft\/yii2\/base\/Controller.php(149): yii\\web\\Controller->beforeAction(Object(yii\\base\\InlineAction))",

...



did you implement findIdentityByAccessToken method? Can you post your code?