首页/ phph5/ 甜趣蛋糕 · 烘焙蛋糕店 PHP+H5 商城 + 商家后台/ 代码预览

💻 代码预览 · 甜趣蛋糕 · 烘焙蛋糕店 PHP+H5 商城 + 商家后台

ph_mall · 上传者 驰旭教育
← 返回详情
r117/core/Controller.php · 约 1270 字符
1<?php2/**3 * 控制器基类:json 输出 / 输入解析4 */5class Controller {6    protected PDO $db;7    public function __construct() { $this->db = Database::pdo(); }89    protected function json($data, int $code = 200): void {10        http_response_code($code);11        header('Content-Type: application/json; charset=utf-8');12        header('Access-Control-Allow-Origin: *');13        echo json_encode($data, JSON_UNESCAPED_UNICODE);14        exit;15    }1617    // 合并 GET/POST/JSON 请求体18    protected function input(string $key = null, $default = null) {19        $raw = file_get_contents('php://input');20        $body = $raw ? json_decode($raw, true) : [];21        if (json_last_error() !== JSON_ERROR_NONE) { $body = []; }22        $all = array_merge($_GET, $_POST, $body);23        return $key === null ? $all : ($all[$key] ?? $default);24    }2526    // 后台只读拦截:开启只读时任何写操作都返回提示27    protected function guardWrite(): void {28        if (defined('ADMIN_READ_ONLY') && ADMIN_READ_ONLY) {29            $this->json(['ok' => 0, 'readonly' => true,30                'msg' => '后台内容只读模式已开启,写操作被拦截。如需修改,请将 config.php 中 ADMIN_READ_ONLY 改为 false。'], 403);31        }32    }33}34