Adding parameters to request then get them doesn't work - php

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

Related

laravel how to call action in Console\Commands

I am using Laravel.
And I have a controller action, which get the orders' information by date. and sum all the amount of orders, and it will return datas.
And I want to call it in Console\Command script so I don't need to repeat the same code, just get those datas from action.
Please help me how to do it?
Do something like this:
Without parameters:
\App::call('App\Http\Controllers\MyController#actionName')
If you have Request parameters:
$request = new \Illuminate\Http\Request($datas);
$controller = app()->make(MyController::class);
$Response = $controller->callAction('ActionName',[$request]);
I wanted to call the CreateNewUser action from the FortifyServiceProvider. It took me some time to find out, but it is quite simple:
use App\Actions\Fortify\CreateNewUser;
// Use fortify to create a new user.
$new_user_action = new CreateNewUser();
$user = $new_user_action->create($input);

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

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

Testing a laravel controller - Faking a Illuminate\Http\Request

I have a controller which is getting an instance of Illuminate\Http\Request injected through the constructor. Now I need to write an unit test that test a call in the controller which uses the values from the Request instance. I have decided to use Faker. How to use Faker to generate an associative array so that I can use the array in my test case like,
$this->post('the_uri','MyFakerArray')
And the dynamic array will automatically be available in my controllers request.
There is no need to fake/mock the Request object.
When you are simulating a request laravel does that for you, it create a request to the url you request and pass the variables, then you get back the response from your application.
For example:
$response = $this->call('POST', '/user', ['name' => 'Taylor']);
now the $response variable has the data to test on.
I think you have:
getContent() // for getting the reponse body
getCode() // for http code: 200, 401 etc
When you do that, your tests should work with the response, you have no need for Faker in this situation.

ZF2 - Get current URL in Controller

This seems like it should be a simple task. I need the current URL from a function within the Controller. This function can be called from multiple actions, and the end goal is to set a form's action attribute. (Side note: It appears IE does not send an ajax request if the URL starts with '#').
I feel like my google-fu is off today because I could not find a good way to do this Zend Framework 2. I have this line currently, but it feels very bulky:
$this->url()->fromRoute(
$this->getServiceLocator()
->get('Application')
->getMvcEvent()
->getRouteMatch()
->getMatchedRouteName()
);
Couldn't you just get the URI from the request object:
$this->getRequest()->getUriString()
Provided your controller extends Zend\Mvc\Controller\AbstractActionController.
Note: This would output the entire URL, like so:
http://example.com/en/path/subpath/finalpath?test=example
If your request route is like this:
http://example.com/en/path/subpath/finalpath?test=example
And You want only this:
/en/path/subpath/finalpath?test=example
You can simply do : $this->getRequest()->getRequestUri()
To specify my Request object is an instance of
\ZF\ContentNegotiation\Request

JSON view in Recess Framework

I am trying to create an api with Recess and I have a question about its JsonView. Currently, if I do a GET request on, for example, /users/1 (which routes to a function that gets all the details for the user with id 1 and responds with Json), I get the following:
{"users":{"id":"1","username":null,"password":null,"datejoined":false}}
How can I make it so that I get the following instead:
{"id":"1","username":null,"password":null,"datejoined":false}
That is, I don't want all the details wrapped inside "users":{}.
By default, Recess's JsonView responds with the properties of your controller. So your $users property is getting directly encoded into JSON.
You can override this by returning a custom response object:
return new OkResponse($this->request, (array)$this->users);
Not sure about recess specifically, but if you are using JsonView method/function with an input parameter (array) $result, then changing $result to $result['users'] may give you the answer you are looking for.
For example using plain PHP:
first object: echo json_encode($result);
second object: echo json_encode($result['users']);

Categories