Edit: I think i found a post that sort of explains how to do it but i could use some help there trying to figure out my usecase: Type hinting $request->getAttribute("woof") / data from middleware - #10 by reuben.helms
This is an old topic, but I am also trying to implement a custom request. I want autocomplete in my IDE for the data that is being sent with the specific request. So I wlll have a LoginUserRequest, SaveInvoiceRequest, etc.
All examples simplified ofcourse.
I want my controller to look something like this:
public function __invoke(
LoginUserRequest $request, // instead of ServerRequestInterface
ResponseInterface $response
): ResponseInterface {
$user = ($this->LoginUserAction)($request);
return $response->withJson([
'success' => true,
'data' => [
'token' => $token,
],
]);
}
and my LoginUserAction something like this:
public function __invoke(LoginUserRequest $data): User
{
return $this->userModel->findByEmail($loginData->email);
}
But how do i go about creating that custom request class? Is there any way in Slim? I tried finding a solution but havent found any yet.
And the custom LoginUserRequest something like this but i have no idea what to extend, implement or use to get there:
class LoginUserRequest
{
public function __construct(
public string $email,
public string $password,
) {}
public function __invoke($request) {
$parsedBody = $request->getParsedBody();
$this->email = $parsedBody['email'];
$this->password = $parsedBody['password'];
return $this;
}
}