search

Home  >  Q&A  >  body text

Set TYPO3's cookie in controller action

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粉037880905P粉037880905295 days ago355

reply all(1)I'll reply

  • P粉041758700

    P粉0417587002024-02-04 17:42:53

    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;

    reply
    0
  • Cancelreply