Set TYPO3's cookie in controller action
P粉037880905
P粉037880905 2024-02-03 20:29:14
0
1
361

I tried setting the cookie in a regular controller action called via typenumcall. I am using TYPO3 v 10.4

public function redirectCookieAction(): ResponseInterface
{
    //do magic stuff...      

    /** @var \TYPO3\CMS\Core\Http\Response $response */
    $response = GeneralUtility::makeInstance(ResponseFactory::class)->createResponse(200);
    $response->withHeader('Set-Cookie', 'cookiename' . '=' . 'cookievalue' . '; Path=/; Max-Age=' . (time()+60*60*24*30));
    return $response;
}

I tried using PSR7-HTTP-Response, but for some reason the cookie was not set after calling the action. It looks like the $response object is completely ignored. How to use ResponseInterface correctly?

I have seen this thread but it is not in the middleware and there is no fe_session at this time: TYPO3 How to set a custom cookie in the form organizer

P粉037880905
P粉037880905

reply all(1)
P粉041758700

Since $response returns a new instance of itself, you must assign it to a variable as shown below

/** @var \TYPO3\CMS\Core\Http\Response $response */
$response = GeneralUtility::makeInstance(ResponseFactory::class)->createResponse(200);
$response = $response->withAddedHeader('Set-Cookie', 'cookiename' . '=' . 'cookievalue' . '; Path=/; Max-Age=' . (time()+60*60*24*30));
return $response;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template