Просмотр файла app/Controllers/Controller.php

Размер файла: 1Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use Psr\Http\Message\ResponseInterface as Response;
  8.  
  9. /**
  10. * Controller
  11. */
  12. abstract class Controller
  13. {
  14. /**
  15. * Creates a JSON response.
  16. *
  17. * @param Response $response
  18. * @param mixed $data
  19. * @param int $status
  20. * @param int $flags
  21. *
  22. * @return Response
  23. */
  24. protected function json(Response $response, mixed $data, int $status = 200, int $flags = 0): Response
  25. {
  26. $response->getBody()->write((string) json_encode($data, $flags));
  27.  
  28. return $response->withStatus($status)->withHeader('Content-Type', 'application/json');
  29. }
  30.  
  31. /**
  32. * Creates a redirect response.
  33. *
  34. * @param Response $response
  35. * @param string $url
  36. * @param int $status
  37. *
  38. * @return Response
  39. */
  40. protected function redirect(Response $response, string $url, int $status = 302): Response
  41. {
  42. return $response->withStatus($status)->withHeader('Location', $url);
  43. }
  44. }