26 lines
742 B
PHP
26 lines
742 B
PHP
<?php
|
|
namespace ProVM\Common\Define;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
abstract class Controller {
|
|
public function withJSON(Response $request, $data, bool $format = true): Response {
|
|
if (!is_string($data)) {
|
|
$enc = 0;
|
|
if ($format) {
|
|
$enc = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES;
|
|
}
|
|
$data = json_encode($data, $enc);
|
|
}
|
|
$request->getBody()->write($data);
|
|
return $request
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withStatus(201);
|
|
}
|
|
public function withRedirect(Response $request, string $uri, int $status = 301): Response {
|
|
return $response
|
|
->withHeader('Location', $uri)
|
|
->withStatus($status);
|
|
}
|
|
}
|