Building and send a request from a method to another in the same controller
In a controller, I have two methods:
public function ProcessFoo(Request $request) {
$draft = $request->input('draft');
...
$r = new Illuminate\Http\Request(['foo' => $draft]);
$this->SendFoo($r);
}
public function SendFoo(Request $request) {
$foo = $request->input('foo');
...
}
Usually, only SendFoo() is called by API users, but sometimes, the foo object needs to be processed, so the API user would call ProcessFoo() which would process it and then call SendFoo(). But SendFoo() needs a request.
Is it good practice to build and send a request from a method to another in the same controller or is there a better way to deal with it ?
This is a bad practice. Would work but controller methods should call other methods only if they are private.
I would move one of those methods to a Service class, and as @vincent15000 said above, would leave only one controller method with if sratement, whether to call that service or not.
I have no issue with using different endpoints but both methods should call some other class that is responsible for sendingfoo, gets only the parameters it needs and returns status (not passing around the request object)
Your controller methods can then inspect the status and return appropriately to the client