Laravel 5.3 - POST is polluted with GET values - php

back in the (real) days, we used to use $_GET, $_POST! and now we got Laravel's \Request::input(). Consequently here is whats happening:
if(\Request::isMethod('post'))
{
$POST = \Request::input();
}
if I have the a variable in $_GET, the value gets in the POST as well.
For example:
&x=1 //i.e. in the query string
$_POST['x'] = null; //as it was not posted with the form, but it could be as there is a field with same name
$POST['x'] = 1; //as its in the GET, but should be null as its not in the $_POST!
Any solution to get POSTed vars only? Or shall I just use $_POST?
Thanks

I believe the only way to get this from the Request instance is to access either the query (GET) or request (POST) property. These are both ParameterBag instances, so you'll probably use the ->get() method of them to access the parameter you want.

Related

CodeIgniter 4 request.getVar function not working

In my codignitor project I have the following view
public function index(){
$whatever = $this->request->getVar("value");
}
I have similar code all over my project, which was 100% working until today, when suddenly it stopped
now $whatever is NULL
However if I just change the code to use:
$whatever = $this->request->getGet();
$result = $whatever['value'];
result will be equal to the value....
Here value is a GET parameter in the url like: example.com?value=1
According to the docs:
"The getVar() method will pull from $_REQUEST, so will return any data
from $_GET, $POST, or $_COOKIE."
when I checked the value of $_GET I see my parameter as expected.
Is this a bug in codeignitor? (the strange thing is it only suddenly stopped working)
I ended up submitting an issue on github here: https://github.com/codeigniter4/CodeIgniter4/issues/4418
which was reportedly solved here:
https://github.com/codeigniter4/CodeIgniter4/pull/4493
Description In the current version, when the request is the get method
and the content-type is json, the $_REQUEST parameter cannot be
obtained
apparently it was an issue with that specific version (v4.0.4) that is now fixed.
In my case the $this->request->getVar() was not working inside models. As per the documentation of CodeIgniter 4:
"If you are not within a controller, but still need access to the application’s Request object, you can get a copy of it through the Services class"
So you would access REQUEST variables like:
$request = \Config\Services::request();
$some_field = $request->getVar('field_name'); // or getPost for $_POST array
Hope it would help others

Set value in $_POST variable to validate through Codeigniter

I'm creating the WebSocket based application with Codeigniter. So, data is coming as a JSON string(not a POST Method). I want to use the Codeigniter's built-in form_validation method to validate the data received as JSON.
this is what I tried so far to set the variable so I can access it through $this->input->post('variable_name') but.
Try #1
$_POST['variable_name'] = !isset($data['variable_name']) ? NULL : $data['variable_name'];
Try #2
$variable_name = !isset($data['variable_name']) ? NULL : $data['variable_name'];
$this->form_validation->set_value("variable_name", $variable_name);
But when I use $this->input->post('variable_name')
it returns NULL.
Here is how I achieved it. Although, It doesn't seems to be an accurate solution but its simple and there is no work around.
I added only one line before execution of form validations
$_SERVER["REQUEST_METHOD"] = "POST";
and then set the variable like this
$_POST['post_var'] = "value";
Now set the validation rules and perform validation.
that's it :)
There's a method called set_data() that was introduced specifically to allow validation of non-POST inputs.
http://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post

Laravel 5 get only GET or POST params from request

I can access request params using Request::input() or Request::all().
The problem is that my request includes both GET and POST params, but only GET ones are used to calculate signature.
Is there a way to retrieve only a set of GET or a set of POST params from request in Laravel 5.1?
Or going with $_GET and $_POST is my only option here?
Thank you.
You can use Request::query() to get only GET parameters. Keep in mind that there are no guaranties about consistency in the order of parameters you get from GET, so you might need to sort the array before calculating the signature - depending on how you calculate the signature.
If you need something straightforward you can just use the global helper:
$pathData = request()->path(); <br />
$queryData = request()->query(); <br />
$postData = array_diff(request()->all(), request()->query());
https://laravel.com/docs/5.6/requests
Follow these instructions to extend the Laravel Request class with your own:
https://stackoverflow.com/a/30840179/517371
Then, in your own Request class, copy the input() method from Illuminate\Http\Request and remove + $this->query->all():
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all();
return data_get($input, $key, $default);
}
Bingo! Now in a POST request, Request::query() returns the query (URL) parameters, while Request::input() only returns parameters from the form / multipart / JSON / whatever input source.

Zend Framework $_REQUEST equivalent

I need to capture several parameters in a controller regardless of whether the were posted or they are in the url.
Does $this->_request->getParam('parameter') work regardless?
To make life easier and shorter code, you can use the _getParam function in your controllers:
$page = $this->_getParam('page', 1);
Note that the second function variable is the default value if the request didn't include that specific variable.
Short answer, yes.
If you are in the controller, you can access any POST of GET parameter by accessing the getParam() method like you said.
$this->getRequest()->getParam("foo") will get the parameter foo, if it is present in the URL via a get param, or in a POST. It will also get any user set parameters.
The
$this->getRequest()->getParams();
Will get several parameters regardless of the action type being sent (get or post).
$this->getRequest()->getParam('foo');
Will get you individual requested parameter.
i prefer always use short function:
$parameter = $this->_getParam('parameter');

Zend Framework - How to modify a single parameter from the request object

A submitted form on my site returns an array of request data that is accessible with
$data = $this->getRequest();
This happens in the controller which gathers the data and then passes this array to my model for placing/updating into the database.
Here is the problem. What if I want to manipulate one of the values in that array? Previously I would extract each value, assigning them to a new array, so I could work on the one I needed. Like so:
$data = $this->getRequest();
$foo['site_id'] = $data->getParam('site_id');
$foo['story'] = htmlentities($data->getParam('story'));
and then I would pass the $foo array to the model for placing/updating into the database.
All I am doing is manipulating that one value (the 'story' param) so it seems like a waste to extract each one and reassign it just so I can do this. Additionally it is less flexible as I have to explicitly access each value by name. It's nicer to just pass the whole request to the model and then go through getting rid of anything not needed for the database.
How would you do this?
Edit again: Looking some more at your question what I am talking about here all goes on in the controller. Where your form`s action will land.
Well you have a couple of options.
First of all $_GET is still there in ZF so you could just access it.
Second there is:
$myArray = $this->_request->getParams();
or
$myArray = $this->getRequest()->getParams();
Wich would return all the params in an array instead of one by one.
Thirdly if the form is posted you have:
$myArray = $this->_request()->getPost();
Wich works with $this->_request->isPost() wich returns true if some form was posted.
About accessing all that in your view you could always just in controller:
$this->view->myArray = $this->_request->getParams();
edit: right I taught you meant the view not the model. I guess I do not understand that part of the question.
If you want to deal with the post data inside your model just:
$MyModel = new Model_Mymodels();
$data = $this->_request->getParams();
$data['story'] = htmlentities($data['story']);
$myModels->SetItAll($data);
And then inside your model you create the SetItAll() function (with a better name) and deal with it there.
Edit: oh wait! I get it. You hate sanytising your input one by one with your technique. Well then what I showed you about how to access that data should simplify your life a lot.
edit:
There is always the Zend_Form route if the parameters are really coming from a form. You could create code to interface it with your model and abstract all this from the controller. But at the end of the day if you need to do something special to one of your inputs then you have to code it somewhere.

Categories