Slim: How to set cookie in slim3?

Created on 22 Jun 2015  路  19Comments  路  Source: slimphp/Slim

Hi guys,

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

thanks someone.

Most helpful comment

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)
            );
        }

All 19 comments

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.

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.

Thanks @Codexing @silentworks

Since June, Its not solved, what a slow service

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

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)
            );
        }

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

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

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.

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

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

I'm using request from container

@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.

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

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)

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 ?

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;
    }
}

Thanks @akrabat I'll try

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GonozalIX picture GonozalIX  路  21Comments

l0gicgate picture l0gicgate  路  50Comments

feryardiant picture feryardiant  路  20Comments

derekjones picture derekjones  路  19Comments

l0gicgate picture l0gicgate  路  31Comments