Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KLM113's avatar

Can you programmatically build a request?

I have a method that was initially coded to process a form, it receives form data through an Illuminate\Http\Request parameter.

Now I'm in the need to use the same method from another class, the problem is I get the data from a different source, not from a Request object.

I was wondering if I can programmatically build the object so I can simply pass it to my method, rather than having to do all the refactoring.

0 likes
10 replies
KLM113's avatar

Yes thanks, I'm already trying that

martinbean's avatar

@klm113 Why not actually fix the problem instead of “hacking” your way around it?

Change the method to be agnostic on how it receives its data. Instead of expecting a request, have it just receive an array of data if that’s what it needs. Then you can use it with a request or data from your other source.

Writing “hacks”—especially to “synthesise” building a request—is just going to make your code less flexible and harder to work with.

KLM113's avatar

@MARTINBEAN Because it's a project where I already spent the development time the customer paid for, I was looking for a quick fix to get working a change they wanted to add in the last minute. Also, I'm not sure how hacky can this be, seems to be pretty straightforward.

martinbean's avatar

@klm113 Then tell you customer how much it will take to accommodate their last minute change properly.

seems to be pretty straightforward.

If it was straightforward then you wouldn’t be asking here ;)

You have a duty to deliver a quality project to your paying client. So refactor your code to make it reusable with a data source other than a form request like you need it to be.

martinbean's avatar

@klm113 You could spend your time actually rewriting your code instead of spending it arguing with me on here…

It’s “hacky” because you’re asking how to create a “fake” request to pass to your class, instead of just modifying your class to not expect a request and instead just accept the data it needs, no matter the source.

KLM113's avatar

@martinbean It seems you have an opinion, but no real arguments to back it up. Topic closed.

martinbean's avatar

@klm113 I’ve given you reasons why it’s “hacky“. You just seem to be upset I’ve suggested you actually fix the problem instead of giving you a block of code to “quick fix” your way around situation.

Given you opened this thread nearly 24 hours ago, a better use of your time would have been to actually refactor your code to accommodate your predicament instead of arguing with me. You’ve wasted both your time and your paying customer’s time in doing so.

1 like

Please or to participate in this conversation.