在 null 上调用成员函数 getDb()
Call to a member function getDb() on null
我正在使用 Yii2 框架和 dektrium/yii2-user
模块在 restful 环境中处理身份验证。
这是我的 API 目录结构:
api
├── config
│ ├── api.php
│ ├── bootstrap.php
│ ├── params.php
│ └── routes.php
├── index.php
├── modules
│ └── v1
│ ├── controllers
│ │ └── UserController.php
│ ├── models
│ │ └── User.php
│ └── Module.php
└── runtime
api.php
...
$config = [
...
'components' => [
'user' => [
'identityClass' => 'app\api\modules\v1\models\User',
'enableAutoLogin' => false,
],
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => require(__DIR__ . '/routes.php'),
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=DBname',
'username' => 'DBusername',
'password' => 'DBpassword',
'charset' => 'utf8'
],
],
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
'controllerNamespace' => 'app\api\modules\v1\controllers',
],
],
...
];
...
UserController.php
class UserController extends ActiveController
{
public $modelClass = 'app\api\modules\v1\models\User';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
];
return $behaviors;
}
}
User.php
use dektrium\user\models\User as Base;
class User extends Base
{
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['auth_key' => $token]);
}
}
当我尝试通过 curl 发出 HTTP POST 请求时:
curl -H 'Accept:application/json' -H "Authorization: Bearer <Token>" http://<url>/api/v1/users
我得到的结果是:
"name": "Exception",
"message": "Call to a member function getDb() on null",
"code": 0,
"type": "Error",
"file": "<project path>/vendor/dektrium/yii2-user/traits/ModuleTrait.php",
"line": 28,
ModuleTrait.php中的第 28 行:
public static function getDb()
{
return \Yii::$app->getModule('user')->getDb();
}
当我检查每一步的结果时,我发现 \Yii::$app->getModule('user')
的值是 null
。
你知道如何解决这个错误吗?
您没有 user
模块 - 您可以尝试添加一个有效的模块:
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
'controllerNamespace' => 'app\api\modules\v1\controllers',
],
'user' => [
'class' => 'dektrium\user\Module',
'admins' => ['your_admin_username'],
'enableRegistration' => false,
'enableConfirmation' => false,
'mailer' => ['welcomeSubject' => 'welcome in my application '],
],
// ...
],
// ...
我正在使用 Yii2 框架和 dektrium/yii2-user
模块在 restful 环境中处理身份验证。
这是我的 API 目录结构:
api
├── config
│ ├── api.php
│ ├── bootstrap.php
│ ├── params.php
│ └── routes.php
├── index.php
├── modules
│ └── v1
│ ├── controllers
│ │ └── UserController.php
│ ├── models
│ │ └── User.php
│ └── Module.php
└── runtime
api.php
...
$config = [
...
'components' => [
'user' => [
'identityClass' => 'app\api\modules\v1\models\User',
'enableAutoLogin' => false,
],
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => require(__DIR__ . '/routes.php'),
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=DBname',
'username' => 'DBusername',
'password' => 'DBpassword',
'charset' => 'utf8'
],
],
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
'controllerNamespace' => 'app\api\modules\v1\controllers',
],
],
...
];
...
UserController.php
class UserController extends ActiveController
{
public $modelClass = 'app\api\modules\v1\models\User';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
];
return $behaviors;
}
}
User.php
use dektrium\user\models\User as Base;
class User extends Base
{
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['auth_key' => $token]);
}
}
当我尝试通过 curl 发出 HTTP POST 请求时:
curl -H 'Accept:application/json' -H "Authorization: Bearer <Token>" http://<url>/api/v1/users
我得到的结果是:
"name": "Exception",
"message": "Call to a member function getDb() on null",
"code": 0,
"type": "Error",
"file": "<project path>/vendor/dektrium/yii2-user/traits/ModuleTrait.php",
"line": 28,
ModuleTrait.php中的第 28 行:
public static function getDb()
{
return \Yii::$app->getModule('user')->getDb();
}
当我检查每一步的结果时,我发现 \Yii::$app->getModule('user')
的值是 null
。
你知道如何解决这个错误吗?
您没有 user
模块 - 您可以尝试添加一个有效的模块:
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
'controllerNamespace' => 'app\api\modules\v1\controllers',
],
'user' => [
'class' => 'dektrium\user\Module',
'admins' => ['your_admin_username'],
'enableRegistration' => false,
'enableConfirmation' => false,
'mailer' => ['welcomeSubject' => 'welcome in my application '],
],
// ...
],
// ...