Controller traits
This commit is contained in:
20
common/Define/Controller/Json.php
Normal file
20
common/Define/Controller/Json.php
Normal 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);
|
||||
}
|
||||
}
|
12
common/Define/Controller/Redirect.php
Normal file
12
common/Define/Controller/Redirect.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user