CHttpRequest及其行为没有名为“getrawbody”的方法或闭包.(Yii v1.1.8)


CHttpRequest and its behaviors do not have a method or closure named "getRawBody" (Yii v1.1.8)

我正在运行Yii v1.1.8(不是我所知道的最新版本,但它是一个遗留项目)。当我运行以下代码时,我得到以下错误?

CHttpRequest and its behaviors do not have a method or closure named "getRawBody".

我运行的代码如下:

function actionDoSomething() {
   $requestBody = Yii::app()->request->getRawBody();
}

我可以看到RawBody()函数从v1.1.13可用-所以我认为我的版本应该没有任何问题?有什么想法,为什么我得到这个错误?http://www.yiiframework.com/doc/api/1.1/CHttpRequest getRawBody-detail

正如评论中所说,对于您的yii版本,您不能使用此方法,因此您应该扩展CHttpRequest来添加它:

components文件夹中创建HttpRequest文件

class HttpRequest extends CHttpRequest
{
    /**
     * Returns the raw HTTP request body.
     * @return string the request body
     * @since 1.1.13
     */
    public function getRawBody()
    {
        static $rawBody;
        if($rawBody===null)
        $rawBody=file_get_contents('php://input');
        return $rawBody;
    }
}

并在配置文件中指定必须使用哪个请求组件

'components' => array(
    'request'=>array(
        'class' => 'HttpRequest',
    ),
),