当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

I am running Yii v1.1.8 (not the latest I know but it is a legacy project). I am getting the following error when I run the following code?

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

The code I am running is as follows:

function actionDoSomething() {

$requestBody = Yii::app()->request->getRawBody();

}

I can see the RawBody() function is available from v1.1.13 - so I assue my version shouldn't have any issue with this? Any ideas on why I am getting this error?

http://www.yiiframework.com/doc/api/1.1/CHttpRequest#getRawBody-detail

网友答案:

As said in the comments, with you yii version you can't use this method, so you should extend CHttpRequest to add it:

Create a HttpRequest file in your components folder

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;
    }
}

And in your configuration file specify wich request component must be used

'components' => array(
    'request'=>array(
        'class' => 'HttpRequest',
    ),
),
分享给朋友:
您可能感兴趣的文章:
随机阅读: