Php Yii2面向对象模型API在执行时抛出错误
我正在使用Yii2框架创建一个api。但它在执行过程中抛出错误 BankDetailsController.php BankDetails.phpPhp Yii2面向对象模型API在执行时抛出错误,php,yii2,yii2-advanced-app,yii2-model,yii2-validation,Php,Yii2,Yii2 Advanced App,Yii2 Model,Yii2 Validation,我正在使用Yii2框架创建一个api。但它在执行过程中抛出错误 BankDetailsController.php BankDetails.php 任何帮助都会很好。。。。谢谢显然getUserIdFromToken($token)接受$token参数,该参数应该是字符串,然后传递给那里$request->getHeaders()这是对象。$token=$request->headers->get('YOUR_-token_-KEY')@leninhasda感谢您的回复。我已经按照您的回答尝试过
任何帮助都会很好。。。。谢谢显然
getUserIdFromToken($token)
接受$token
参数,该参数应该是字符串,然后传递给那里$request->getHeaders()代码>这是对象。$token=$request->headers->get('YOUR_-token_-KEY')代码>@leninhasda感谢您的回复。我已经按照您的回答尝试过了,现在我得到了如下“消息”所示的错误:“传递给yii\\web\\User::login()的参数1必须实现接口yii\\web\\IdentityInterface,null给定,在第17行的C:\\xampp\\htdocs\\app\\common\\components\\CustomAuth.php中调用并定义”,@列宁哈斯达,你能帮我一下吗?@Bizley,请检查一下这个错误。。。传递给yii\\web\\User::login()的参数1必须实现接口yii\\web\\IdentityInterface,给定null,在第14行的C:\\xampp\\htdocs\\app\\common\\components\\CustomAuth.php中调用,并明确定义getUserIdFromToken($token)
接受$token
参数,该参数应该是字符串,然后传递给那里$request->getHeaders()代码>这是对象。$token=$request->headers->get('YOUR_-token_-KEY')代码>@leninhasda感谢您的回复。我已经按照您的回答尝试过了,现在我得到了如下“消息”所示的错误:“传递给yii\\web\\User::login()的参数1必须实现接口yii\\web\\IdentityInterface,null给定,在第17行的C:\\xampp\\htdocs\\app\\common\\components\\CustomAuth.php中调用并定义”,@列宁哈斯达,你能帮我一下吗?@Bizley,请检查一下这个错误。。。传递给yii\\web\\User::login()的参数1必须实现接口yii\\web\\IdentityInterface,给定null,在第14行的C:\\xampp\\htdocs\\app\\common\\components\\CustomAuth.php中调用并定义
<?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,
}