Cookie Handler or Middleware for Slim 3

I was looking inside Slim 3 and found Slim\Http\Cookies class which may be intended to use in Slim 3 but it never completed and neither mentioned by anyone.
Recently I looked for cookie handler and many of you suggested for dflydev/fig-cookies but I think this library doesn’t work in PHP 7.0 and that’s a big issue to me as I am making applications that run on PHP 7.0 or later.

I would like to get suggestions from you for a good library that works in PHP7.0 otherwise I have to create a new one.

Thanks in advance

Vanilla PHP Cookies of course work always and everywhere. I’m joking :wink:

The most cookie libraries I know are not good maintained anymore or too complex…
except the Symfony Http Foundation library. This library works also very well with Sessions in Slim.

use Symfony\Component\HttpFoundation\Cookie;  
$cookie = new Cookie('color', 'green', strtotime('tomorrow'), '/', 
   'somedomain.com', true, true);

What about samesite in setcookie()??

What about samesite in setcookie()??

I think “native” samesite cookies works only with PHP 7.3+.

Maybe you try this with Slim under PHP 7.0:

use Symfony\Component\HttpFoundation\Cookie;

//...

$response = $handler->handle($request);

$cookie = new Cookie(
    'color',
    'green',
    strtotime('tomorrow'),
    '/',
    null,
    true,
    true,
    false,
    Cookie::SAMESITE_LAX
);

$response = $response->withAddedHeader('Set-Cookie', (string)$cookie);

return $response;

1 Like

I am trying to use middleware to update the response.
I am using a different approach where a cookie class holds the value and update it using middleware