dflydev / dflydev-fig-cookies

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

facing isssue #17

Closed ibrahimch closed 1 year ago

ibrahimch commented 8 years ago

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

I've try set with response too, but same

harikt commented 8 years ago

PSR-7 is immutable so you cannot pass the request and response and assume the passed ones have not changed. Without seeing how the code is used, we cannot say the implementation is wrong. If you want to prove this is wrong, send a PR with tests .

Thank you.

ibrahimch commented 8 years ago

I'm using it with slim3 $c['cookies'] = new Cookies\Cookies($app->getContainer()->request, $app->getContainer()->response); $app->get('test', function($req, $res){ echo $this->cookies->get('theme'); });

simensen commented 8 years ago

I agree with @harikt, looking at your code it is clear to me that it is never going to work as expected. Every time you call a method to set a cookie value you need to capture the response object and ensure that that response object is returned by the application otherwise you will never actually be setting that cookie. Because of that, get will always be empty (the set cookie header was never actually sent to the client so it will never send you a cookie value as a part of a request).

simensen commented 8 years ago

Said another way, it is not possible to create a wrapper object like this and expect it to work. The FigResponseCookies and FigRequestCookies classes were designed to make these objects easier to deal with. If you want the value of a request's cookie, you should just ask the copy of the request object you have at that time using:

FigRequestCookies::get($request, 'theme')

If you want to set a cookie value on a response, you should just set it on the copy of the response you have at that time, and make sure that the new value is returned.

Looking at this more closely, it looks like you are using FigRequestCookie to set the value on the request. If your intention is to set a cookie to be sent back to the client, you should be using FigResponseCookie and making sure that you use the return value (a modified $response) from that point forward instead of the original response instance.

ibrahimch commented 8 years ago

PS. I've tried it with request response too, But same error