我正在通过用户ID创建一个由YII2用户api获取用户的API。如果我传递用户ID,那么它工作正常但是如果我传递字符串然后我得到以下错误。
<pre>An Error occurred while handling another error:
yii\base\InvalidParamException: Response content must not be an array. in /var/www/html/vendor/yiisoft/yii2/web/Response.php:1020
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/web/Response.php(313): yii\web\Response->prepare()
#1 /var/www/html/vendor/yiisoft/yii2/web/ErrorHandler.php(128): yii\web\Response->send()
#2 /var/www/html/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request: categories/wewqewqe in /var/www/html/vendor/yiisoft/yii2/base/Controller.php:127
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('wewqewqe', Array)
#1 /var/www/html/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('categories/wewq...', Array)
#2 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 /var/www/html/index.php(16): yii\base\Application->run()
#4 {main}
Next yii\web\NotFoundHttpException: Page not found. in /var/www/html/vendor/yiisoft/yii2/web/Application.php:114
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /var/www/html/index.php(16): yii\base\Application->run()
#2 {main}</pre>
我使用swagger
执行API,而API的端点是
users/{id}
输入
时工作正常/users/1
时出错
/users/asdf
Web.php
'users/<id:\d+>' => 'users/',
答案 0 :(得分:2)
我最近遇到了同样的问题,并且已经通过配置文件夹中web.php中的路由更改解决了这个问题。
更改您的网址
users/<id>
代替users/<id:\d+>.