Laravel 5 get only GET or POST params from request - php

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.

Related

Laravel 5.3 - POST is polluted with GET values

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.

codeigniter rerouting url with some new additional parameters

I am using codeigniter re-route to clean up some urls.
I am aware that I can do
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
But in some cases I have to add some extra parameters to the redirect url so that I get them as a param to the method. for example
$route['product_unique_and_rare'] = "catalog/product_lookup_by_id/{HERE I WANT SOME ADDITIONAL EXTRA PARAM}";
How to do this so that I get the value in the param of the method rather than the value in uri->resegment
you can try this
$route['product_unique_and_rare/(:num)'] = "catalog/product_lookup_by_id/$1";
Get the param in product_lookup_id like this
function product_lookup_id($product_id){
/*$product_id will be the passed parameter*/
}
So, if someone goes for http://domain.com/product_unique_and_rare/23, $product_id will get the value 23.
You can hard-code the parameter too, but I believe you aren't looking for that.

pass data to request method of HttpSocket in CakePHP

I'm using the HttpSocket request method of CakePHP to perform some requests to some api. I want to send some parameters with my request. Does anybody know how I can send some parameters using that method? For example, let's say I'm sending a request to this url:
http://www.mydomain.com
but I wanna send parameter to this request for example:
username: smith123
password: qwerty
If I were to do this with Ajax, I would do something like:
$.post('http://www.mydomain.com', {username: "smith123", password: "qwerty"}, 'json');
How do I send those parameters (also the type of data expected as specified in the ajax example above) to http://www.mydomain.com using the request method of the HttpSocket class of CakePHP
Please help
Thank you
I think you would want to use the get or post method, not request, which is the base method.
http://book.cakephp.org/1.3/en/view/1518/get
You can pass parameters in the second argument of the get method as either a string or an array:
App::import('Core', 'HttpSocket');
$HttpSocket = new HttpSocket();
$results = $HttpSocket->get('http://www.google.com/search', 'q=cakephp');
debug($HttpSocket->response);

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: Can i just get GET params?

In Zend Framework, most of the time to get a param, i will use
// from controller
$this->getRequest()->getParam('key');
but how can i get just GET params using the 'Zend' way? Or do i just use $_GET? Is there any difference between
$this->getRequest()->getParam('key');
vs
$_GET['key'];
Use getQuery():
$this->_request->getQuery('key');
Other methods available include
getParam()
getQuery()
getPost()
getCookie()
getServer()
getEnv()
getParam() checks user params first, then $_GET, and then $_POST, returning the first match found or null.
Try to avoid accessing the superglobals directly.
The main difference is that
$_GET['key'];
is a dependency on the environment. It requires the superglobal to be available and containing a key of that name. It's also just a simple array access, while
$this->getRequest()->getParam('key');
is an API method call. Access to the Request is abstracted. There is no dependency on the actual environment. The Request object could be a mock. The getParam method will always return a value regardless whether it is from $_GET or $_POST.
Putting an abstraction on top of the Request is better, because it allows for more decoupling, less dependencies and therefor makes your application easier to test and maintain.
This works for ZF2
$this->params()->fromQuery('key', 1); // second argument is optional default paramter
After studying Zend 2's in depth data binding documentation, I've found that it is best to access parameters from the route via the automatically accessible Params plugin. Utilizing this plugin, you can get a parameter as shown below from within a controller.
$this->params('key');
In Zend Framework 1 there are two possibilities to define "visible" parameters.
https://subdomain.domain.tld(/module)/controller/name/parameter1/value1
https://subdomain.domain.tld(/module)/controller/name/?parameter2=value2
First parameter1 is part of the URL path and second parameter2 is a real GET parameter. Both will be returned if calling $request->getParams(). But only parameter2 is returned when using $request->getQuery(). Because parameter1 is not part of the query. It's part of the url, logical.
Now, I like the answer of Ryan Chouinard how to get the parameter2 with $request->getQuery(). But parameter1 behaves like a GET parameter and I want to treat them like that. So how can I get the visible parameter1 and parameter2 but not the additional hidden parameter3 from the post data?
My only solution is a helper that clones the request and changes the so called paramSources (default: ['_GET', '_POST']) to ['_GET'] and then use getParam() as regular...
/**
* #param Zend_Controller_Request_Http $request
* #param string $param
* #param mixed|null $default
*
* #return mixed|null
*/
function getVisibleParam(string $param, $default = null, Zend_Controller_Request_Http $request = null)
{
if (
!$request &&
!($request = Zend_Controller_Front::getInstance()->getRequest())
) {
throw new \RuntimeException('There is no request. Are you in a wrong context?');
}
$_request = clone $request;
$_request->setParamSources(['_GET']);
return $_request->getParam($param, $default);
}

Categories