- 添加环境配置文件 .env.example 包含数据库、JWT、CORS等配置 - 创建 .gitignore 文件忽略敏感文件和临时文件 - 配置 Apache 重写规则支持路由转发 - 实现 JWT 认证中间件提供用户身份验证功能 - 添加 MySQL 数据库初始化脚本包含分组、图片、笔记表结构
24 lines
775 B
PHP
24 lines
775 B
PHP
<?php
|
|
|
|
namespace App\Middleware;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
|
|
use Slim\Psr7\Response;
|
|
|
|
class CorsMiddleware
|
|
{
|
|
public function __invoke(Request $request, RequestHandler $handler): Response
|
|
{
|
|
$response = $handler->handle($request);
|
|
|
|
$origin = $_ENV['CORS_ORIGIN'] ?? '*';
|
|
|
|
return $response
|
|
->withHeader('Access-Control-Allow-Origin', $origin)
|
|
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
|
|
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
|
|
->withHeader('Access-Control-Allow-Credentials', 'true');
|
|
}
|
|
}
|