Set values in $_POST in Yii2 on request recieved? - php

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()

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']]);

I think you should consider refactoring your code a bit, especially if you are not the only person working on the project because artificially adding values to $_POST is just confusing and should be avoided, if possible. If I see a code that reads a variable from $_POST, I go looking for it being set on the frontend not somewhere in the controller.
You could make your interceptor do:
$post = Yii::$app->request->post();
// or $post = _ $POST;
$post['foo'] = 'bar';
someNamespace::$writeData = $post;
Then when you want to access the data (assuming it doesn't always go through the interceptor and needs to be initialized when empty):
if (empty(someNamespace::$writeData)) {
someNamespace::$writeData = $_POST;
}
$data = someNamespace::$writeData;
and read everything from that static variable instead of $_POST. It's neater and much more maintanable code, IMHO.

Just to extend on the accepted answer of #rob006, in response to the comment below that by Budi Mulyo.
You can add to the post data by doing the following:
$params = Yii::$app->request->getBodyParams();
$params['somethingToAdd'] = 'value'
Yii::$app->request->setBodyParams($params);
Still not sure if you want or need to do this, but it is possible :)

Related

Accessing POST parameters from web form using Request and not $_POST Symfony 3

I have a web form that submits some hidden input fields with post parameters and I want to use Request object from Symfony.
This is how I'm doing it now using an API based request also with Postman app I can access the son values like this.
myAction(Request $request){
$content = $request->getContent();
$params = json_decode($content, true);
$value = $params['value'];
}
But when i use a web form it doesn't get the values this way. I was trying to figure out how to get the values and i ended up using the post variable which works fine.
$value = $_POST['value'];
I don't want to use the global variable but rather grab the value from the request. I don't have a super good reason why, other than I prefer it the Request way. Any help would be appreciated.
Is there something special I'd have to do with the HTML form?
Use $value = $request->request->get('value'); to get a single POST value.
Use $values = $request->request->all() to get all POST values.
From symfony docs

php how to send get and post and cookie at once?

How to send get&post&cookie at once?
if($_GET[get] && $_POST[post] && $_COOKIE[cookie])
{
//blah blah
}
How to send get,post,cookie data for that code at once??
give me a sample code please!
I used form tag but couldnt send at once but I don't know how to send it at once
Sounds like you want $_REQUEST - this is by default "An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE."
Be aware that by using $_REQUEST you lose information about where the variable came from, so it is recommended to use the individual variables, or set $_REQUEST to only contain GET and POST data, with POST overwriting GET: $_REQUEST = array_merge($_GET, $_POST);

Extract URL value with CakePHP (params)

I know that CakePHP params easily extracts values from an URL like this one:
http://www.example.com/tester/retrieve_test/good/1/accepted/active
I need to extract values from an URL like this:
http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
I only need the value from this id:
id=1yOhjvRQBgY
I know that in normal PHP $_GET will retrieve this easally, bhut I cant get it to insert the value into my DB, i used this code:
$html->input('Listing/vt_tour', array('value'=>$_GET["id"], 'type'=>'hidden'))
Any ideas guys?
Use this way
echo $this->params['url']['id'];
it's here on cakephp manual http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params
You didn't specify the cake version you are using. please always do so. not mentioning it will get you lots of false answers because lots of things change during versions.
if you are using the latest 2.3.0 for example you can use the newly added query method:
$id = $this->request->query('id'); // clean access using getter method
in your controller.
http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query
but the old ways also work:
$id = $this->request->params->url['id']; // property access
$id = $this->request->params[url]['id']; // array access
you cannot use named since
$id = $this->request->params['named']['id'] // WRONG
would require your url to be www.example.com/tester/retrieve_test/good/id:012345.
so the answer of havelock is incorrect
then pass your id on to the form defaults - or in your case directly to the save statement after the form submitted (no need to use a hidden field here).
$this->request->data['Listing']['vt_tour'] = $id;
//save
if you really need/want to pass it on to the form, use the else block of $this->request->is(post):
if ($this->request->is(post)) {
//validate and save here
} else {
$this->request->data['Listing']['vt_tour'] = $id;
}
Alternatively you could also use the so called named parameters
$id = $this->params['named']['id'];

Is it possible to get all post variables in ExpressionEngine, like you could in CodeIgniter?

In a controller in CI you could get all post variables by doing something like this:
$data = $this->input->post();
In EE (built off of CI by the same people) the analogous syntax would be:
$data = $this->EE->input->post();
The only issue is that instead of an array with all of the data, you get a boolean of false.
Is there some way of getting an array of all post data, using ExpressionEngine rather than the POST superglobal?
Thanks.
Try native
$this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$this->input->post(); // returns all POST items without XSS filter
Ref: https://www.codeigniter.com/user_guide/libraries/input.html
Ok, the way to get results similar to CI within EE for all elements of a POST, while still leveraging the security features of EE is the following:
foreach($_POST as $key => $value){
$data[$key] = $this->EE->input->post($key);
}
Since you can access POST vars by name, looping through them in $_POST, then explicitly calling each will yield the desired result.

how do i reset the value of a single session array index in codeigniter?

Using a user model which returns an array that looks like this:
$user_data['display_name'] = "John Doe";
$user_data['avatar'] = ./images/user144.jpg";
i create my session using $this->session->set_userdata('user_data',$user_data);
now if on another controller i let the user change his avatar,
how can i replace the session variable associated to that?
like $this->session->set_userdata('user_data["avatar"]',$new_avatar);
just wont work right?
hee thanks for the help...
From looking at an overview of your code, I'm guessing the best way to go about this is to unset the data and reset it.
Use $this->session->unset_userdata('thesessiontounset');
Then set it back up with the new information and old.
The session->set_userdata() function will only let you modify one key at a time. In your case the key refers to an array so what you're trying to do isn't possible in the way you're attempting to do it.
When I'm updating my session I run something like this.
//Create or setup the array of the fields you want to update.
$newFields = array('avatar' = > 'image01.png');
//Check to see if the session is currently populated.
if (!is_array($this->session->userdata('abc'))){
//...and if it's not - set it to a blank array
$this->session->set_userdata('abc',array());
}
//Retrieve the existing session data
$existing_session = $this->session->userdata('abc');
//Merge the existing data with the new data
$combined_data = array_merge($this->session->userdata('abc'), $newFields);
//update the session
$this->session->set_userdata('abc',$combined_data);
More details on array_merge can be found here
First controller
$user_data['display_name'] = "John Doe";
$user_data['avatar'] = "./images/user144.jpg";
$this->session->set_userdata('user_data',$user_data);
Second controller
$user_data = $this->session->userdata('user_data');
$user_data['avatar'] = $new_avatar;
$this->session->set_userdata('user_data', $new_avatar);
It is a bit late, but it might be useful to someone else, this seems to work:
$this->session->userdata['user_data']['avatar'] = $new_avatar;
$this->session->userdata['other_data']['other'] = $other;
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.
For Unset Session variable
$this->session->unset_userdata('avatar');
For Set Session variable
$this->session->set_userdata('avatar', '/images/user144.jpg"');
Use just like this
$this->session->set_userdata('session_var',"");

Categories