RESTFull 用户登录问题 [ 2.0 版本 ]
目前实现的登录是这样的:
public function actionLogin(){
$request = Yii::$app->request;
if(!$request->isPost){
return ['status'=>'fail','msg'=>'请求方式 错误'];
}
$model = new UserModel();
if (Yii::$app->request->post('user_name')) {
// if ($model->load(Yii::$app->getRequest()->getBodyParams())) {
$user_name = $request->post('user_name');
$user_pwd = $request->post('user_pwd');
$res = $model->login($user_name,$user_pwd);
return $res;
}
return ['status'=>'fail','msg'=>'load 错误'];
}
public function login($user_name,$user_pwd)
{
if ($this->validate()) {
$user = self::findByUser_name($user_name);
if($user['user_pwd'] == md5($user_pwd)){
return ['status'=>'success','user_id'=>$user['user_id'],'user_realname'=>$user['user_realname'],'access_token'=>$user['access_token']];
}else {
return ['status'=>'fail','msg'=>'密码错误'];
}
} else {
return ['status'=>'fail','msg'=>'账户或者密码错误'];
}
}
以上只是解决了登录问题,并没有用YII的登录方法,导致,使用Yii::$app->user->username
获取不到用户信息,求助各位同仁
$model->load(Yii::$app->getRequest()->post())
,load不到提交的数据
共 5 个回答
-
$model->login怎么实现的
共 7 条回复public function login($user_name,$user_pwd) { if ($this->validate()) { $user = self::findByUser_name($user_name); if($user['user_pwd'] == md5($user_pwd)){ return ['status'=>'success','user_id'=>$user['user_id'],'user_realname'=>$user['user_realname'],'access_token'=>$user['access_token']]; }else { return ['status'=>'fail','msg'=>'密码错误']; } } else { return ['status'=>'fail','msg'=>'账户或者密码错误']; } }
-
johnny1991 回答于 2017-06-28 23:30 举报
rest一般使用access-token登陆
public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'authenticator' => [ 'class' => CompositeAuth::className(), 'authMethods' => [ QueryParamAuth::className(), ], ] ]); }
access-token存放在user标里面的access_token这个字段里面
共 1 条回复lasmai 觉得很赞 -
$model->load(Yii::$app->getRequest()->post(),'') 第二个参数为空就OK
共 2 条回复 -
qqa4560354 回答于 2017-06-30 17:45 举报
public function actionIndex(){
$loginModel = new Login(); $loginModel->load(['Login'=>Yii::$app->request->post()]); if ($loginModel->validate ()) { $rs = $loginModel->login (); } else { return Helper::format_data ( PARAM_ERROR, $loginModel->getErrors () ); } }
共 1 条回复
陈诺 北京
注册时间:2017-06-26
最后登录:2018-03-20
在线时长:5小时51分
最后登录:2018-03-20
在线时长:5小时51分
- 粉丝4
- 金钱610
- 威望0
- 积分660