我正在 Slim 框架中创建一个用于 XSS 保护的中间件。 这是我的代码:
$input = $request->getParsedBody();
array_walk_recursive($input, function(&$input) {
$input = htmlspecialchars($input);
});
$request->merge($input); // Not able to merge $input into $request
return $next($request, $response, $next);
清理请求后,我想合并到 slim http 请求中,这样当我使用 $request->getParsedBody() 时,我应该获得清理数据。
use Slim\Factory\AppFactory;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Response;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->post('/merge-array', function (Request $request, Response $response, $args) {
// Get the current request body
$body = $request->getParsedBody();
// Your array to merge into the request body
$newData = [
'key1' => 'value1',
'key2' => 'value2',
];
// Merge the new data with the existing request body
$mergedBody = array_merge($body, $newData);
// Create a new request with the merged body
$newRequest = $request->withParsedBody($mergedBody);
// Forward the new request to the next middleware or route handler
return $handler->handle($newRequest);
});
$app->run();