dflydev / dflydev-fig-cookies

Cookies for PSR-7 HTTP Message Interface.
MIT License
224 stars 29 forks source link

Usage with Slim 3 #8

Closed skizzo closed 8 years ago

skizzo commented 8 years ago

Hi, would be greate to have some tutorials or links on how to use this in combination with Slim 3. Their upgrade guide refers to this archive, but I can't find a single tutorial on how to properly implement it with Slim. Cheers and thanks, Steff

harikt commented 8 years ago

It doesn't matter whether you use slim or expressive as long as it is psr-7 .

See how to get a cookie from a request https://github.com/dflydev/dflydev-fig-cookies#get-a-request-cookie and set a response cookie : https://github.com/dflydev/dflydev-fig-cookies#set-a-response-cookie

Hope the documentation is clear about it.

GAlduini commented 8 years ago

@skizzo: Hi, i'm using this with Slim 3, and it's working fine. What's exactly your problem? The only suggestion without any other information is about the remove method: as with the php function setcookie(), to remove a (response) cookie I suggest to modify the cookie itself setting an expiration time in the past.

Delallen commented 8 years ago

Ello,

I'm building a web application using Slim 3, and as soon as I attempt to use this package my application completely stops working. It doesn't throw a PHP error or anything. I've instantiated the class, but when developing, as soon I do the below snippet, the application returns a blank page.

$request = FigRequestCookies::set(Cookie::create('thisCookie', 'this_Value'));

Var dumping the $request variable doesn't display anything either.

A stripped down version of the code for ref. https://gist.github.com/Delallen/942133dcdbb8297e316fa75c7085123e

Are there any specific PHP Extensions and/or modules that need to be used in order to get this to work correctly?

I've tested this on another pc (Mac Mini running PHP v.5.5.9), and it seems to be working correctly locally, but on my Digital Ocean server(PHP 5.5.9-1ubuntu4.16) and on a Mac Book (PHP 5.5.31), nothing works. I'm compiling a list of installed PHP extensions on all machines to see the discrepancies, but wanted to see if anyone has encountered this previously.

harikt commented 8 years ago

@Delallen seems you missed to import FigRequestCookies , don't know whether it is the error for you mentioned it worked on some systems. May be you are keeping a different version of the code in gist.

Edit : Also the signature looks a bit different.

See https://github.com/dflydev/dflydev-fig-cookies#set-a-request-cookie

Delallen commented 8 years ago

@harikt well Derp, looks like I erased part of it on one of my commits. Thanks.

simensen commented 8 years ago

I'm closing this as it looks like @harikt fielded this one correctly? @Delallen if this is incorrect let me know and I can reopen!

ibrahimch commented 8 years ago

Hello guys, I'm facing issue, my code is below

`namespace Cookies; use Dflydev\FigCookies\FigResponseCookies; use Dflydev\FigCookies\FigRequestCookies; use Dflydev\FigCookies\Cookie; use Dflydev\FigCookies\SetCookie;

class Cookies

{ protected $req; protected $res;

public function __construct($req, $res)
{
    $this->req = $req;
    $this->res = $res;
}

function get($name)
{
    $response = $this->res;
    $request = $this->req;
    return FigRequestCookies::get($request, 'theme');
}

function set($name, $value = "", $expire = 0)
{
    $response = $this->res;
    $request = $this->req;
    $request = FigRequestCookies::set($request, Cookie::create('theme', 'blue'));
}

}`

When I call get cookies, this returns theme= I'm going to die, if this will not solve

ibrahimch commented 8 years ago

I've try with request and response method, but same problem

ecaradec commented 6 years ago

I came here after looking for how to use FigCookies with SlimPHP. SlimPHP expects that you return a response object if you change it.

$app->get('/cookie', function($request, $response) use($app) {
    $c = new Cookies($request, $response);

    echo $c->get('name');

    $c->set('name', 'value');

    return $response;  // don't forget this
});

Also you must be careful to not use a copy of the response object but the original one, so you want to pass it as a reference to the Cookies class.

class Cookies {
    public function __construct(&$req, &$res)
    {
        $this->req = &$req;
        $this->res = &$res;
    }
}