Controller traits

This commit is contained in:
2020-07-02 01:06:39 -04:00
parent 8272c71c34
commit 4d26000a26
2 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace ProVM\Common\Define\Controller;
use Psr\Http\Message\ResponseInterface as Response;
trait Json {
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);
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace ProVM\Common\Define\Controller;
use Psr\Http\Message\ResponseInterface as Response;
trait Redirect {
public function withRedirect(Response $request, string $uri, int $status = 301): Response {
return $response
->withHeader('Location', $uri)
->withStatus($status);
}
}