r/PHPhelp • • Jan 21 '25

How decouple method from request?

How can I decouple a method from a request? I’d like to pass a generic object (e.g., a DTO) instead of being tied to a request. This way, I could call that specific method both from an API and from a command. What would be the best approach to achieve this? Thank you

1 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/Ok-Fisherman7532 Jan 22 '25

In the way you've described, i am forced to call another controller, whereas i already have an API route pointing directly to the createSignatureRequest method of the DriController. Is there no way to achieve this without invoking an additional controller? Thanks

1

u/equilni Jan 22 '25 edited Jan 22 '25

createSignatureRequest(SignatureRequestData $data)

I am going by your code example, which is looking for SignatureRequestData. If you want to pass the Request, then the parameter needs to change to createSignatureRequest(Request $request). then create the SignatureRequestData object later in the method.

class DriController extends Controller
{
    public function __construct(
        private DigitalSignatureManager $digitalSignatureManager
    ) {}

    public function createSignatureRequest(Request $request)
    {
        $sigRequest = new SignatureRequestData($request);
    }
}