使用TYPO3中间件时如何添加cookie?

How to add a cookie when using TYPO3 Middleware?

我想使用中间件添加cookie。

在 TYPO3 中我有 Psr\Http\Message\ServerRequestInterface $requestPsr\Http\Server\RequestHandlerInterface $handler 变量。

添加具有所有所需设置(安全、域、过期)的 cookie 的最佳做法是什么?

PSR 中对此没有明确的接口,因为它基本上归结为在 $response 中发送 Set-Cookie header。您可以自己构建 header 或使用一些为您执行此操作的软件包:

我现在的工作代码是:

$cookie = \Dflydev\FigCookies\SetCookie::create($name)
    ->withValue($value)
    ->withDomain($request->getAttribute('site')->getBase()->getHost())
    ->withSecure(true);

$response = new \TYPO3\CMS\Core\Http\RedirectResponse(
    (string)$request->getUri(),
    302,
    ['Set-Cookie' => (string)$cookie]
);

设置了 cookie,我重定向了访问者,这样 cookie 也可以被读出,例如TypoScript 条件。