ufm
41 строка · 1.0 Кб
1<?php declare(strict_types=1);
2
3namespace Upside\Ufm\Controller;
4
5use Upside\Http\Response;
6use Upside\Http\Status;
7use Upside\View\ViewInterface;
8
9abstract class BaseController
10{
11protected ViewInterface $view;
12
13public function init(ViewInterface $view): void
14{
15$this->view = $view;
16}
17
18public function json(mixed $data = '', Status $status = Status::OK): Response
19{
20return new Response(
21\json_encode($data, JSON_THROW_ON_ERROR),
22$status,
23['Content-Type' => 'application/json']
24);
25}
26
27public function redirect(string $url, Status $status = Status::FOUND): Response
28{
29return new Response('', $status);
30}
31
32public function redirect_to_route(string $route_name, Status $status = Status::FOUND): Response
33{
34return new Response('', Status::FOUND);
35}
36
37public function render(string $view, array $parameters = [], Status $status = Status::OK): Response
38{
39return new Response($this->view->render($view, $parameters), $status);
40}
41}
42