我正在使用Yii2框架创建一个api。但它在执行过程中会抛出错误。
<?php
namespace backend\controllers;
use yii\rest\ActiveController;
use common\components\CustomAuth;
class BankDetailsController extends ActiveController
{
public $modelClass = 'common\models\BankDetails';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CustomAuth::className(),
];
return $behaviors;
}
}
public function getUserIdFromToken($token){
$sql = "select AdminUserID from utpl_opt_usertoken where auth_token =".$token;
$userIdFromToken = Yii::$app->db->createCommand($sql)->queryAll();
return $userIdFromToken;
}
<?php
namespace common\models;
use yii\db\ActiveRecord;
use Yii;
class BankDetails extends ActiveRecord
{
public static function tableName()
{
return 'th_bank_details';
}
public function fields()
{
return [ 'bank_name', 'branch_name' , 'ifsc_code' , 'account_type' , 'account_number' , 'name_on_account' , 'relationship' , 'user_id' , 'is_active' , 'listing_mode' , 'listing_user' , 'created_by' ];
}
public function rules()
{
return [
[
['bank_name' , 'branch_name', 'account_type' , 'user_id' , 'is_active' , 'listing_mode' , 'listing_user', 'created_by' ], 'safe'
],
[
[ 'ifsc_code' , 'account_number' , 'name_on_account' , 'relationship' ], 'required'
],
];
}
public function save($runValidation = true, $attributeNames = null)
{
$id = Yii::$app->user->getId();
$this->created_by = $id;
return $this->insert($runValidation, $attributeNames);
}
}
<?php
namespace common\components;
use yii\filters\auth\AuthMethod;
use common\models\Admin;
class CustomAuth extends AuthMethod
{
public function authenticate($user, $request, $response)
{
//var_dump($request->getHeaders());
$token = $request->getHeaders();
$userId = Admin::getUserIdFromToken($token);
$identity = Admin::findIdentity($userId);
$user->login($identity);
return $identity;
return true;
}
}
执行时,这会抛出错误
{
"name": "PHP Recoverable Error",
"message": "Object of class yii\\web\\HeaderCollection could not be converted to string",
"code": 4096,
"type": "yii\\base\\ErrorException",
"file": "C:\\xampp\\htdocs\\app\\common\\models\\Admin.php",
"line": 98,
}
任何帮助都会很棒....谢谢