Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set values in $_POST in Yii2 on request recieved?

I am writing a interceptor to validate request and decode data received from POST. After decoding data I have to set data to $_POST so that my all previous writer functionality will work as it is.

I have set values like below

$_POST['amount'] = $data['a'];
$_POST['currency'] = $data['c'];

I am able to get these variables using $_POST but these values are not accessible in Yii::$app->request->post()

So my question is can I get these values by Yii::$app->request->post()

like image 704
Aabir Hussain Avatar asked Dec 18 '18 09:12

Aabir Hussain


1 Answers

Post data is cached inside of Request component, so any changes in $_POST will not be reflected in Yii::$app->request->post(). However you may use setBodyParams() to reset this cache:

Yii::$app->request->setBodyParams(null);

$post = Yii::$app->request->post();

Or just use setBodyParams() to set your data directly without touching $_POST:

Yii::$app->request->setBodyParams(['amount' => $data['a'], 'currency' => $data['c']]);
like image 55
rob006 Avatar answered Sep 22 '22 17:09

rob006