解决yii2中 Class yii/web/JsonParser does not exist, ReflectionException问题

Posted 勾践

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决yii2中 Class yii/web/JsonParser does not exist, ReflectionException问题相关的知识,希望对你有一定的参考价值。

最近在调试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问题的主要内容,如果未能解决你的问题,请参考以下文章

YII2 内置独立操作

yii2高级框架怎么整合前台html页面

慕课网,我的yii2学习笔记(基础篇)

[moka同学笔记]Yii2 自定义class自定义全局函数(摘录)

yii2-basic后台管理功能开发之五:用户权限的控制

yii2 Rbac实例 (做完以下这些 会有些小的报错,相信各位都能解决,大多数都是自己命名空间上的问题)。