覆盖特定操作的 Content-Type

用例:只有一个应该按原样返回普通(文本)内容的操作:

public function actionAsXML()
{
    $this->layout = false;
    Yii::$app->response->format = Response::FORMAT_XML;

    return ['aaa' => [1, 2, 3, 4]];;
}

预定义的响应格式为:

  • FORMAT_HTML
  • FORMAT_XML
  • FORMAT_JSON
  • FORMAT_JSONP
  • FORMAT_RAW

对于 text/plain 没有开箱即用的 mime 类型,请改用:

public function actionPlainText()
{
    $this->layout = false;
    Yii::$app->response->format = Response::FORMAT_RAW;
    Yii::$app->response->headers->add('Content-Type', 'text/plain');

    return $this->render('plain-text'); // outputs template as plain text
}