如何在 Yii2 REST 控制器中 return 空白主体?
How to return blank body in Yii2 REST Controller?
我尝试 return 我的休息控制器中的空白主体,但它 returns 'null'.
我已经尝试在脚本的最后一行使用
\Yii::$app->response->setStatusCode(200);
并得到相同的结果。
我使用高级模板和自定义休息逻辑。
控制器扩展 yii\rest\Controller
main.php
文件中有我的配置
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'api\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'enableSession' => false,
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'GET add' => 'api/add',
'GET feed' => 'api/feed',
'GET remove' => 'api/remove',
],
],
],
'params' => $params,
];
这是我的动作
public function actionAdd()
{
try
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$request = \Yii::$app->request;
$id = $request->get('id');
$user = $request->get('user');
$secret = $request->get('secret');
if(!$id || !$user || !$secret)
{
$this->setStatus(500);
return [
'error' => 'missing parameter'
];
}
if(!$this->checkSecret($secret, [$id, $user]))
{
$this->setStatus(500);
return [
'error' => 'access denied'
];
}
$existing = Subscribers::find()->where(['user' => $user])->count();
if($existing)
{
$this->setStatus(500);
return [
'error' => 'user already exists in database'
];
}
$subscriber = new Subscribers();
$subscriber->user = $user;
//$subscriber->save();
/*
* expected 200 OK and blank body
* returns 200 OK and 'null'
*/
return \Yii::$app->response->setStatusCode(200)->send();
}catch (\Exception $exception)
{
$this->setStatus(500);
return [
'error' => 'internal error'
];
}
}
我没有任何行为,也许我应该?
空字符串无效 JSON,因此 return 使用空 body 和 content-type: application/json
header 的响应是不正确的。在这种情况下,您可能希望使用 Response::FORMAT_RAW
而不是 Response::FORMAT_JSON
:
Yii::$app->response->format = Response::FORMAT_RAW;
return;
这将 return 清空 body 和 content-type: text/html
header。
但是如果你真的想假装你的响应是JSON和return空body,你可以直接设置$content
属性-这个将跳过响应的格式:
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->content = '';
return;
我尝试 return 我的休息控制器中的空白主体,但它 returns 'null'.
我已经尝试在脚本的最后一行使用
\Yii::$app->response->setStatusCode(200);
并得到相同的结果。
我使用高级模板和自定义休息逻辑。
控制器扩展 yii\rest\Controller
main.php
文件中有我的配置
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'api\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'enableSession' => false,
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'GET add' => 'api/add',
'GET feed' => 'api/feed',
'GET remove' => 'api/remove',
],
],
],
'params' => $params,
];
这是我的动作
public function actionAdd()
{
try
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$request = \Yii::$app->request;
$id = $request->get('id');
$user = $request->get('user');
$secret = $request->get('secret');
if(!$id || !$user || !$secret)
{
$this->setStatus(500);
return [
'error' => 'missing parameter'
];
}
if(!$this->checkSecret($secret, [$id, $user]))
{
$this->setStatus(500);
return [
'error' => 'access denied'
];
}
$existing = Subscribers::find()->where(['user' => $user])->count();
if($existing)
{
$this->setStatus(500);
return [
'error' => 'user already exists in database'
];
}
$subscriber = new Subscribers();
$subscriber->user = $user;
//$subscriber->save();
/*
* expected 200 OK and blank body
* returns 200 OK and 'null'
*/
return \Yii::$app->response->setStatusCode(200)->send();
}catch (\Exception $exception)
{
$this->setStatus(500);
return [
'error' => 'internal error'
];
}
}
我没有任何行为,也许我应该?
空字符串无效 JSON,因此 return 使用空 body 和 content-type: application/json
header 的响应是不正确的。在这种情况下,您可能希望使用 Response::FORMAT_RAW
而不是 Response::FORMAT_JSON
:
Yii::$app->response->format = Response::FORMAT_RAW;
return;
这将 return 清空 body 和 content-type: text/html
header。
但是如果你真的想假装你的响应是JSON和return空body,你可以直接设置$content
属性-这个将跳过响应的格式:
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->content = '';
return;