Yii2 REST API无法正常工作

时间:2017-04-07 12:16:39

标签: php rest yii2 swagger

我正在通过用户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-&gt;prepare()
#1 /var/www/html/vendor/yiisoft/yii2/web/ErrorHandler.php(128): yii\web\Response-&gt;send()
#2 /var/www/html/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler-&gt;renderException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: yii\base\ErrorHandler-&gt;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-&gt;runAction(&#039;wewqewqe&#039;, Array)
#1 /var/www/html/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module-&gt;runAction(&#039;categories/wewq...&#039;, Array)
#2 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application-&gt;handleRequest(Object(yii\web\Request))
#3 /var/www/html/index.php(16): yii\base\Application-&gt;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-&gt;handleRequest(Object(yii\web\Request))
#1 /var/www/html/index.php(16): yii\base\Application-&gt;run()
#2 {main}</pre>

我使用swagger执行API,而API的端点是

users/{id}

输入

时工作正常
/users/1

时出错
/users/asdf

Web.php

'users/<id:\d+>' => 'users/',

1 个答案:

答案 0 :(得分:2)

我最近遇到了同样的问题,并且已经通过配置文件夹中web.php中的路由更改解决了这个问题。

更改您的网址 users/<id>代替users/<id:\d+>.