- 添加环境配置文件 .env.example 包含数据库、JWT、CORS等配置 - 创建 .gitignore 文件忽略敏感文件和临时文件 - 配置 Apache 重写规则支持路由转发 - 实现 JWT 认证中间件提供用户身份验证功能 - 添加 MySQL 数据库初始化脚本包含分组、图片、笔记表结构
50 lines
1.0 KiB
PHP
50 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Utils;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
class Database
|
|
{
|
|
private static $instance = null;
|
|
private $connection;
|
|
|
|
private function __construct()
|
|
{
|
|
$config = require __DIR__ . '/../../config/database.php';
|
|
|
|
$dsn = sprintf(
|
|
"mysql:host=%s;port=%s;dbname=%s;charset=%s",
|
|
$config['host'],
|
|
$config['port'],
|
|
$config['database'],
|
|
$config['charset']
|
|
);
|
|
|
|
try {
|
|
$this->connection = new PDO(
|
|
$dsn,
|
|
$config['username'],
|
|
$config['password'],
|
|
$config['options']
|
|
);
|
|
} catch (PDOException $e) {
|
|
throw new \Exception("数据库连接失败: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public static function getInstance()
|
|
{
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function getConnection()
|
|
{
|
|
return $this->connection;
|
|
}
|
|
}
|