The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Cookie in Slim 3

dewey992's Avatar

dewey992

12 Dec, 2015 03:29 PM

How would I manage cookies wit Slim 3? Currently my setup is:

$c['cookie'] = function ($c) {
  return new \Slim\Http\Cookies;
};

Then somewhere (like middleware) I just need to access like:

$this->cookie->get($key) // For getting cookie
$this->cookie->set($key, $value) // For setting cookie

However, the only problem is how could I delete the cookie? In \Slim\Http\Cookies I can't find any method related to 'delete' as 'deleteCookie()' in Slim 2. Thanks!

  1. 1 Posted by Andrew G. on 17 Dec, 2015 06:08 PM

    Andrew G.'s Avatar

    How that object in app container can modify your response? You don't show it

  2. 2 Posted by Andrew G. on 17 Dec, 2015 06:28 PM

    Andrew G.'s Avatar

    You can do it in the old way:

    // Set expires in past
    $app->cookie->set('test', [
         'expires' => date('Y-m-d H:i:s', time() - 36000)
     ]);

    // Modify response with cookies
    $response = $response->withHeader('Set-Cookie', $app->cookie->toHeaders());

  3. 3 Posted by dewey992 on 17 Dec, 2015 07:38 PM

    dewey992's Avatar

    Thanks! Will try it soon

  4. 4 Posted by Dustin H on 29 Dec, 2015 07:43 PM

    Dustin H's Avatar

    Your \Slim\Http\Cookies object won't contain any of the cookie data from the request. You must pass it the cookie parameters from the request. Use this to define the service:

    $container['cookie'] = function($c){
        $request = $c->get('request');
        return new \Slim\Http\Cookies($request->getCookieParams());
    };
    

    I'm not really sure why this service isn't defined automatically by Slim. Hope this helps.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac