Laravel Request::create not working with Request::input from CLI - php

I need to call a method from CLI which uses the Request::input so I had to manually create the Request object and the below code is not working in my case. can you please advice.
$request = \Illuminate\Support\Facades\Request::create('script/run', 'POST');
\Illuminate\Support\Facades\Route::dispatch($request);
$request->request->add(['adjustment' => '10']);
I am trying to call the value from another class
dd(\Illuminate\Support\Facades\Request::input('adjustment'));
output shows as null but it supposed to show the value 10
I also tried the below option and it did not work as well
$request = app('request');
$request->request->add(['adjustment' => '10']);
dd($request->all());
returns empty array. If I am able to add input here, it will work for me.

You're almost there, but when you create the request you are assigning it to a variable, however, when you go to die and dump the value of 'adjustment' you're calling a new request which doesn't have the value of 'adjustment'
To obtain this simply call it from the request you've assigned to $request.
dd($request->input('adjustment'));

Found the answer finally, I had to use merge instead of add. below code worked for me.
$request = app('request');
$request->merge(['adjustment' => '10']);
dd(\Illuminate\Support\Facades\Request::all());

Related

Adding parameters to request then get them doesn't work

I have Laravel project and in this project I have to create Request object to use it like this:
$request = new Request();
I tried to add from and to dates like this:
$request->request->add([from'=>$from_temp,'to'=>$to_temp]);
when I dd the request it gives me that from and to are exist(image)
image for request Object
but when I try to access them using
request('from')
it gives me null
I don't know what's going on
Thank you
request('from') helper will pull values form the global request object, not from the $request variable that you created. What you want to to is use $request->get('from')

Laravel 8 Testing Method Illuminate\Http\JsonResponse::getSession does not exist

How to get the updated session data from a JsonResponse during test, I want to make use of the updated session data to my next series of test.
I am using an api route, when i make use of $response->dumpSession() the session can be traced. But when I make use of $response->getSession()->all to pass this values to my next test request returns me a error.
when I tried to look for the property of the $response cannot find the property of session
Thanks in advance.
There is no getSession method on the TestResponse class. But instead you can use
app('session.store')->all();
to achieve the same thing. This what the dumpSession uses under the hood also.

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

How to forward with GET parameters?

How to forward with GET parameters?
I have two actions in same controller. I do some magic in the first action, and if all goes well, I want it to forward to the other one, but with GET parameters created in the first one.
What I have now is this:
return $this->forward('AppBundle:default:tracked', array(), array(
'tracking_code' => $tracking_code,
'company' => $tp_company_name
)));
Empty array in there because of a desperate effort to get it to work. Documentation tells that second array is for the query parameters, but nothing really happens, so I must be doing something wrong.
The second action tries to get the parameters like this:
/**
* #Route("/tracked", name="tracked")
*/
public function trackedAction()
{
$request = Request::createFromGlobals();
// If there is the required variables in the GET:
if($request->query->has('tracking_code') && $request->query->has('company'))
But no, variables never get there it seems.
Reason I have this kind of setup, is that user can get into the trackedAction from another place as well.
I think the proper way to get your parameters in your second action would be to do like this :
return $this->forward('AppBundle:default:tracked', array(
'tracking_code' => $tracking_code,
'company' => $tp_company_name
)));
And your second action would be :
/**
* #Route("/tracked/{tracking_code}/{company}", name="tracked")
*/
public function trackedAction($tracking_code=null, $company=null)
{
...
}
I used the $tracking_code = null because you specified this could be accessed from another place, that maybe does not provide these parameters. This way it works for both of them.
Hope this helps.
The way that Symfony controller forwarding works is by duplicating the current the Request with the options that you pass and then re-dispatching it through the HttpKernel component. You can see this in the code. Because it's a sub-request, your second action is creating a Request from globals (i.e. $_GET etc.) which haven't changed.
The solution is to change your second action method signature to:
public function trackedAction(Request $request)
This means that the $request variable will be "local" to your action and will contain the variables you want.
In practice you should always pass the Request in to your actions this way as it makes your controllers a lot more testable and prevents strange issues like this.
Alternatively to all of the above, you could use a redirect instead which would do an HTTP redirect rather than just forwarding within the Symfony request system.

Need to access associative array data created inside a class by json_decode from outside the class

I've been using procedural php for a long time but am still learning OOP. I was able to find a bit of code online that is in the form of a class and I am able to call a new instance of the class and use var_dump on a string to have it print JSON data to a web page. I can look at the results of the var_dump and see that it's returning exactly what I want. I'm then able to use json_decode on the same string to turn it into and associative array and then I can echo the values from within the class. The problem is, I need to use the array values in more code - it's great that I can confirm it by printing it to a web page but I need to use it... but I'm getting errors that state the array is undefined once I try to access it outside of the class.
I'm using this line to convert the data into an array:
$response_array = json_decode($body, true);
I've already confirmed that this works within the class by using this code to print some of the data:
echo $response_array['amount'];
and it works - I see it on the web page.
I've been using this code to create the new instance of the class:
$fdata = new FData();
$fdata->request($order_total, $cc_exp, $cc_number, $cc_name, $order_id, $customer_id);
(the function named 'request' is defined as a public function inside the class)
After that, I want to grab the $response_array so that I can store the returned data into a transactions table, i.e something like this:
$r = mysqli_query($dbc, "CALL add_transaction($order_id, $response_array['transaction_type'], $response_array['amount'], $response_array['exact_resp_code'], $response_array['exact_message'], $response_array['bank_resp_code'], $response_array['bank_message'], $response_array['sequence_no'], $response_array['retrieval_ref_no'], $response_array['transaction_tag'], $response_array['authorization_num'])");
but I keep getting an error saying that the array values are undefined.
Things I have already tried (and which failed) include:
Defining the variables as public inside the class, setting their value in the class, and then calling them outside the class...
public $amount = $response_array['amount'];
then using $amount in my procedure CALL --- I still get the error saying that $amount is undefined.
Using 'return', as in
return $response_array;
and still the error saying the values are undefined.
I tried embedding the entire rest of my code within the class, just copy/paste it in right after the json_decode... but for some reason it can't seem to then make the database calls and other things it needs to do.
I've been reading about __construct but I'm not sure if it applies or how to use it in this case...
I want to stress that I AM able to see the results I want when I use var_dump and echo from within the class.. I need to access the array created by json_decode OUTSIDE of the instance of the class.
Any advice would be greatly appreciated. Thanks.
Assuming your FData::request method ends with something like this...
$response_array = json_decode($body, true);
return $response_array;
and you call it like this...
$response_array = $fdata->request(...);
You should then be able to use $response_array in the calling scope.
Extra note; you should be using prepared statements with parameter binding instead of injecting values directly into your SQL statements.

Categories