TYPO3 如何在表单修整器中设置自定义 cookie

TYPO3 how to set custom cookie inside a form finisher

打字错误3 9.5.8 我们正在实施一个包含多个步骤的时事通讯订阅流程,在提交第二步时,我们在完成器中查询 graphQL 以查看电子邮件是否有效订阅 - 我们使用电子邮件的 "state" 设置了一个 cookie地址。

setcookie("isEmailSubscribable", ($content->data->isEmailSubscribable) ? "1" : "0", time() - 3600, "/", "x.at", false);

我们必须根据写入 cookie 的 "state" 在第三步显示一条消息。但无论我尝试什么,cookie 都没有设置(我猜)。

cookies 和 typo3 是怎么回事?在表单修整器中设置 cookie 是否为时已晚?但如果是,我该如何解决?

非常感谢帮助

整理器内部:

// Set a cookie
$this->getTypoScriptFrontendController()->fe_user->setKey('ses', 'value', 'test');

// Get the cookie
$this->getTypoScriptFrontendController()->fe_user->getKey('ses', 'test');
  • ses 是会话 cookie
  • 用户 此 cookie 需要 fe_user 才能登录

值可以是一个数组,它会序列化存储在数据库中fe_sessions.ses_data


更新:

或者您可以使用 PSR-15 中间件进行尝试:https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/RequestHandling/Index.html

在您的中间件 Class 中,您获得 $request 和 $response 并使用它们来设置或读取 cookie:

// Write a cookie
$response = $response->withAddedHeader(
    'Set-Cookie',
    $cookieName . '=' . $yourValue . '; Path=/; Max-Age=' . (time()+60*60*24*30)
);

// Read a cookie
$request->getCookieParams()[$cookieName];

您只需检查电子邮件地址请求和一个隐藏字段即可检测您的请求是否已提交。