如何在CakePHP 3.5中的控制器级别创建cookie? [英] How to create cookies at controller level in CakePHP 3.5?

查看:63
本文介绍了如何在CakePHP 3.5中的控制器级别创建cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在cookiephp 3.5.x中使cookie正常工作.

I have problems to get cookies to work in cakephp 3.5.x.

在较早的版本中,我使用了Cookie组件,但是现在不建议使用.我还不清楚如何使用这种新的中间件来读取和写入Cookie.

in earlier versions I've used the Cookie component but this is now deprecated. Its unclear for me how to use this new middlewarestuff for reading and writing cookies.

文档 对我来说还不清楚.它向我展示了如何设置cookie中间件,但没有介绍如何在控制器中创建cookie.有没有在3.5.x中处理过Cookie的人?

The documentation is unclear for me. It shows me how to set up the cookie middleware but not how to handle creating cookies in a controller. Is there anyone who has handled cookies in 3.5.x?

推荐答案

中间件仅替换Cookie组件的加密部分(从根本上说,这一直是CakePHP 3.0的唯一操作),如果需要,它将自动加密并解密您已配置的Cookie.

The middleware only replaces the encryption part of the Cookie component (which basically is the only thing it did as of CakePHP 3.0 anyways), if required it automatically encrypts and decrypts the cookies that you've configured.

您不使用中间件来读取或写入cookie,这是通过请求和响应对象完成的,这是CakePHP 3以来的默认设置.

You do not use the middleware to read or write cookies, that is done via the request and response objects, which is the default since CakePHP 3.

从控制器操作中读取和写入cookie可以很简单:

Reading and writing cookies from within a controller action can be as simple as:

$rememberMe = $this->request->getCookie('remember_me');

$this->response = $this->response->withCookie('remember_me', [
    'value' => 'yes',
    'path' => '/',
    'httpOnly' => true,
    'secure' => false,
    'expire' => strtotime('+1 year')
]);

另请参见

这篇关于如何在CakePHP 3.5中的控制器级别创建cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆