【发布时间】:2016-04-05 16:53:40
【问题描述】:
我正在尝试为我的应用程序创建 rest api 以获取我的 android 应用程序中的数据。这是我的控制器
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
use yii\filters\auth\QueryParamAuth;
/**
* Tk103 Controller API
*/
class Tk103Controller extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Tk103CurrentLocation';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => QueryParamAuth::className(),
];
return $behaviors;
}
}
我在 user 表中添加了access_token 列,在用户模型中实现findIdentityByAccessToken() 并调用此 URL
http://localhost:7872/api/v1/tk103s?access-token=abcd
当且仅当access_token 与表中的任何单个用户匹配时,此方法效果很好并返回数据。
检查QueryParamAuth类,发现QueryParamAuth::authenticate()认证成功后返回$identity。
目前这个网址正在返回我的表格的全部数据。
我想要的是(认证后):
- 获取请求者的用户 ID/用户名。
- 根据该 id/username,根据 db 中的表关系,与他相关的数据。 (目前正在返回整行,但我只想要与当前请求者/用户匹配的少数行)
我试过但没有得到任何线索来捕捉身份验证后返回的用户$identity。
我知道也有可能做到这一点。帮助我创造魔法。
【问题讨论】:
标签: rest yii2 restful-authentication