35. <?php
namespace AppHttpResponder;
use IlluminateViewFactory;
use IlluminateHttpResponse;
use IlluminateContractsSupportResponsable;
class IndexResponder implements Responsable
{
public function __construct(Factory $view)
{
$this->view = $view;
}
public function toResponse($request): Response
{
return new Response(
$this->view->make('welcome'), 200
);
}
} 35
36. データを渡す場合
public function setUser(UserEntity $user): self
{
$this->user = $user;
return $this;
}
こういうsetterを用意してViewに渡してあげればOK
public function toResponse($request): Response
{
return new Response(
$this->view->make('welcome')->with(['user' => $this->user]),
200
);
}
36
37. JSONを返す場合
public function toResponse($request): JsonResponse
{
return new JsonResponse(
['user' => $this->user],
200
);
}
IlluminateHttpJsonResponse とか
SymfonyComponentHttpFoundationJsonResponse で返却してあげればOK
37