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

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

ph_mall · 上传者 驰旭教育
← 返回详情
r117/index.php · 约 1884 字符
1<?php2/**3 * 前端控制器 + 极简路由(?c=控制器&a=动作)4 * 例:5 *   /                     → HomeController::index()   吐 H5 首页6 *   /admin.html           → 商家后台面板(public/admin.html)7 *   /?c=api&a=banners     → ApiController::banners()  业务 JSON8 *   /?c=admin&a=login     → AdminController::login()   后台 JSON9 */10require_once __DIR__ . '/config.php';11require_once __DIR__ . '/core/Database.php';12require_once __DIR__ . '/core/Controller.php';13require_once __DIR__ . '/core/Model.php';14require_once __DIR__ . '/core/View.php';1516// 自动加载 app/ 下的控制器与模型(类名 → app/<type>s/<Class>.php)17spl_autoload_register(function ($class) {18    foreach (['controllers', 'models'] as $type) {19        $file = __DIR__ . '/app/' . $type . '/' . $class . '.php';20        if (is_file($file)) { require_once $file; return; }21    }22});2324// 商家后台面板:/admin.html 直接托管 public/admin.html(autoloader 已就绪)25$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);26if ($uri === '/admin' || $uri === '/admin.html') {27    (new HomeController())->admin();28}2930// 默认前端路由:/c=控制器&a=动作31$c = preg_replace('/[^a-z0-9_]/i', '', $_GET['c'] ?? 'home');32$a = preg_replace('/[^a-z0-9_]/i', '', $_GET['a'] ?? 'index');3334$cls = ucfirst(strtolower($c)) . 'Controller';35$file = __DIR__ . '/app/controllers/' . $cls . '.php';36if (!is_file($file)) {37    http_response_code(404);38    header('Content-Type: application/json; charset=utf-8');39    echo json_encode(['ok' => 0, 'msg' => 'controller not found: ' . $cls]);40    exit;41}42require_once $file;43if (!class_exists($cls) || !method_exists($cls, $a)) {44    http_response_code(404);45    header('Content-Type: application/json; charset=utf-8');46    echo json_encode(['ok' => 0, 'msg' => 'action not found']);47    exit;48}49(new $cls())->$a();50