slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

How to set cookie in slim3? #1310

Closed zyx-rd closed 9 years ago

zyx-rd commented 9 years ago

Hi guys,

how to set cookie in slim3? I can't find any information for that.

thanks someone.

Melbournite commented 9 years ago

The service provider isn't currently completed for cookies. When it's ready it'll be here: https://github.com/slimphp/Slim-Http-Cookies

In the meantime I'd suggest using the standard PHP functions. It'll be pretty easy to change over when it is available.

silentworks commented 9 years ago

For cookies use this library for psr7 cookies https://github.com/dflydev/dflydev-fig-cookies, we are likely to create a service provider or some sort of integration to it in the Slim-Http-Cookies repo.

zyx-rd commented 9 years ago

Thanks @Codexing @silentworks

ibrahimch commented 8 years ago

Since June, Its not solved, what a slow service

nisbeti commented 8 years ago

It is solved, Ibrahimch, use https://github.com/dflydev/dflydev-fig-cookies

ibrahimch commented 8 years ago

But I'm facing problem https://github.com/dflydev/dflydev-fig-cookies/issues/17

nisbeti commented 8 years ago

I'm using it within a slim3 route callback, as follows:


use Dflydev\FigCookies\FigRequestCookies;
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
use Carbon\Carbon;

class GetLogout
{
    public function __invoke(Request $request, Response $response)
    {

        $cookie = FigRequestCookies::get($request, 'cookie_name');

        if ($cookie->getValue()) {

            $response = FigResponseCookies::set($response, SetCookie::create('cookie_name')
                ->withValue(null)
                ->withExpires(Carbon::parse()->timestamp - 86400)
                ->withPath($config->get('app.base_path'))
                ->withHttpOnly(true)
            );
        }
ibrahimch commented 8 years ago

I even can't get Or set cookies, you are talking about deleting :santa:

ibrahimch commented 8 years ago

I'm using it with a big project, And using controller+models, so its annoying pass $request, $response param every time, I'm getting it from $container, $app->getContainer()->response/request

$app = new \Slim\App();
$container = $app->getContainer();
$container['cookies'] = new \Model\Cookies($app);
........................
namespace Model;
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($app)
    {
        $this->req = $app->getContainer()->request;
        $this->res = $app->getContainer()->response;
    }

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

    function set($name, $value = "", $expire = 0)
    {
        $response = $this->res;
        $request = FigResponseCookies::set($response, SetCookie::create('theme')
            ->withValue('blue')
            ->withPath('/firewall')
        );
    }
}

But nothing can help me, I've tried get and set function with req, res both, but nothing happens

akrabat commented 8 years ago

You can't use the request/response in the container as they are not the same instances as the ones passed into your route action.

ibrahimch commented 8 years ago

As I know, fig cookies are not working through slim framework, Its standalone project, It needs pr7 http message request/response, so I can serve request/response that container had

akrabat commented 8 years ago

Slim uses PSR-7 request/response objects which is why you cannot use the request from the container.

ibrahimch commented 8 years ago

I'm using request from container

silentworks commented 8 years ago

@ibrahimch this is what @akrabat is saying you cant do. The request from the container is stale, because PSR-7 request/response is immutable.

So a request from the container would not have any values set on the request being passed to the the route callable.

ibrahimch commented 8 years ago

So I can't use It without passing $req, $res out of router ?

akrabat commented 8 years ago

Correct. You must use the request and response that are passed into the route callable (or into the middleware if that's what you're writing)

ibrahimch commented 8 years ago

Is there any other way that how can I get request response something like Slim\Http\Request, Slim\Http\Response located hete vendor\slim\slim\Slim\Http Sorry for asking it again and again Or any other class, package that have Slim Controllers like Laravel ?

akrabat commented 8 years ago

Your controller action has a request object passed to it.

$app->get('/', 'HomeController::homeAction');

class HomeControlller
{
    public function __construct($container) {
        $this->container = $container;
    }

    public function homeAction($request, $response)
    {
        // do something with $request here.
        // set up $response
        return $response;
    }
}
ibrahimch commented 8 years ago

Thanks @akrabat I'll try