| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- declare(strict_types=1);
- namespace Glacier\Core;
- /**
- * 统一响应出口:HTML / JSON / 文本 / 重定向。
- * 输出后立即终止(never)。
- */
- final class Response
- {
- public static function html(string $body, int $status = 200, array $headers = []): never
- {
- http_response_code($status);
- header('Content-Type: text/html; charset=utf-8');
- foreach ($headers as $name => $value) {
- header($name . ': ' . $value);
- }
- echo $body;
- exit;
- }
- public static function json(mixed $data, int $status = 200): never
- {
- http_response_code($status);
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
- exit;
- }
- public static function text(string $body, int $status = 200): never
- {
- http_response_code($status);
- header('Content-Type: text/plain; charset=utf-8');
- echo $body;
- exit;
- }
- public static function redirect(string $location, int $status = 302): never
- {
- http_response_code($status);
- header('Location: ' . $location);
- exit;
- }
- }
|