Create a Laravel Request object on the fly
Asked Answered
E

7

79

I'm handling data in one controller and want to pass it further into another controller to avoid duplicate code.

Is there a way to set up a Request object that is needed in the other controller's store-method? I've traced down the Request inheritance and came to Symfony's Request object which has a request property that is in fact a ParameterBag that holds a method add to add parameters with values to it.

I've tried the following but I'm getting null as result:

$myRequest = new Request();
$myRequest->request->add(['foo' => 'bar']);
var_dump($myRequest->foo);

I'm on Laravel 5.1 for this project.

Estivate answered 26/10, 2016 at 12:57 Comment(1)
To "avoid duplicate code you" you need to abstract the common functionality into a dedicated class, give it a proper mnemonic name, write a set of unit tests around it, and then mock it in controllers when unittesting controllers.Forbidding
W
103

You can use replace():

$request = new \Illuminate\Http\Request();

$request->replace(['foo' => 'bar']);

dd($request->foo);

Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously.

Wherewithal answered 29/10, 2016 at 19:29 Comment(3)
Thanks, due to a time limit I have solved it with a little duplicate code (other things had to happen in the outside-controller as well). I'll remember this for another time!Estivate
can you tell me how we can change the request method to POST ?Irretrievable
@RahulSharma you can do by this way :: Request::create(' any URL that you want to add here ','POST',['title'=>'New Article Published.','body' =>$post->title]);Copybook
A
53

Creating a request object with $myRequest = new Request(); creates the object with method = 'GET'. You can check your request's method with $myRequest->getMethod(). As the request property holds data for POST requests you cannot use $myRequest->request->add() by default. First you have to set the request's method to POST:

$myRequest = new \Illuminate\Http\Request();
$myRequest->setMethod('POST');
$myRequest->request->add(['foo' => 'bar']);
dd($request->foo);

By the way using $myRequest->query->add() you can add data to a GET request.

Archicarp answered 21/2, 2018 at 20:25 Comment(0)
F
44

To "avoid duplicate code" you need to abstract the common functionality into a dedicated class, give it a proper mnemonic name, write a set of unit tests around it, and then mock it in controllers when unittesting controllers.

but if you still need to make requests:

use Illuminate\Http\Request;

$request = new Request([
        'name'   => 'unit test',
        'number' => 123,
    ]);

and if you need the full functionality of the Request, you need to add some extra lines

$request
    ->setContainer(app())
    ->setRedirector(app(\Illuminate\Routing\Redirector::class))
    ->validateResolved();
Forbidding answered 27/3, 2019 at 23:57 Comment(0)
C
2

You can clone the existing request and fill it with new data:

$request = (clone request())->replace(['foo' => 'bar']);
Chambless answered 13/12, 2019 at 23:9 Comment(0)
I
2

You have to change

this

$myRequest = new Request();
$myRequest->request->add(['foo' => 'bar']);
var_dump($myRequest->foo);

to

$myRequest = new Request();
$myRequest->merge(['foo' => 'bar']);
echo $myRequest->input('foo');
Imaret answered 19/9, 2022 at 13:5 Comment(0)
L
1

You can add the request parameter on the fly using these methods.

Replace

replace function doc

If you are in controller then, pass Request object in paramter of the function like

  function createUser(Illuminate\Http\Request $request){
     $request->replace(array_merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"), $request->all()));
}

Merge function

merge function doc

function createUser(Illuminate\Http\Request $request){
     $request->merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"));
}

add function

 function createUser(Illuminate\Http\Request $request){
     $request->request->add(array_merge(array("new_key1"=>"new_value1","new_key_n"=>"new_value_n"), $request->all()));
}

Note:: in all function we are extending the request, mean previous parameter will remain there. You will be adding your own. You can replace them all.

Lionel answered 5/11, 2018 at 11:43 Comment(0)
C
0

I tried all other solutions but no one was working on my side. so Tried a new Soution that is working in my case. It will be useful if you are looking at Randow or New URL. it will not update your existing $request parameters.

 Request::create('URL that you want ','POST',['title'=>'New Article Published.','body' =>$post->title]);

Check the response on the Log File.

enter image description here

Copybook answered 19/3 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.