1. 程式人生 > >解決yii2中 Class yii/web/JsonParser does not exist, ReflectionException問題

解決yii2中 Class yii/web/JsonParser does not exist, ReflectionException問題

index.php yii2 parser 解決 OS quest get cep lec

最近在調試RESTful API示例時,出現以下錯誤:

 1 {
 2     "name": "Exception",
 3     "message": "Class yii/web/JsonParser does not exist",
 4     "code": -1,
 5     "type": "ReflectionException",
 6     "file": "/Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/di/Container.php",
 7     "line": 426,
 8     "stack-trace": [
9 "#0 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/di/Container.php(426): ReflectionClass->__construct(‘yii/web/JsonPar...‘)", 10 "#1 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/di/Container.php(364): yii\\di\\Container->getDependencies(‘yii/web/JsonPar...‘)", 11 "#2 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/di/Container.php(156): yii\\di\\Container->build(‘yii/web/JsonPar...‘, Array, Array)", 12
"#3 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/BaseYii.php(345): yii\\di\\Container->get(‘yii/web/JsonPar...‘, Array)", 13 "#4 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/web/Request.php(531): yii\\BaseYii::createObject(‘yii/web/JsonPar...‘)", 14 "#5 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/rest/CreateAction.php(51): yii\\web\\Request->getBodyParams()", 15
"#6 [internal function]: yii\\rest\\CreateAction->run()", 16 "#7 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/base/Action.php(94): call_user_func_array(Array, Array)", 17 "#8 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/base/Controller.php(157): yii\\base\\Action->runWithParams(Array)", 18 "#9 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/base/Module.php(528): yii\\base\\Controller->runAction(‘create‘, Array)", 19 "#10 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/web/Application.php(103): yii\\base\\Module->runAction(‘deptinfo/create‘, Array)", 20 "#11 /Users/Desktop/workspaces/php/yii2-ims/vendor/yiisoft/yii2/base/Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))", 21 "#12 /Users/Desktop/workspaces/php/yii2-ims/api/web/index.php(10): yii\\base\\Application->run()", 22 "#13 {main}" 23 ] 24 }

其實,這是自己給自己挖的一個坑,結果把自己給陷進去了,下面是配置文件的代碼:

‘request‘ => [
            ‘class‘ => ‘yii\web\Request‘,
            ‘parsers‘ => [
                ‘application/json‘ => ‘yii/web/JsonParser‘,
            ],
        ],

問題就在於配置時將 “yii/web/JsonParser”寫錯了,正確的是 “yii\web\JsonParser”,命名空間應以反斜杠“\”分割,看似很小的問題,結果調試了很長時間才找到原因,所以在寫代碼配置的時候一定要認真仔細,以免被自己誤導,浪費寶貴的時間。

解決yii2中 Class yii/web/JsonParser does not exist, ReflectionException問題