Invalid Call – yii\base\InvalidCallException The cookie collection is read only.
来源:互联网 发布:linux mint 18和ubuntu 编辑:程序博客网 时间:2024/05/19 08:02
Invalid Call – yii\base\InvalidCallException
The cookie collection is read only.
在使用Yii2进行cookie操作时会报以上错误多半是使用了如下的方式来获取cookie容器而导致的
$cookieObj = Yii::$app->request->cookies;
处理方式有两种:
第一种方式,设置readOnly属性为false:
$cookieObj->readOnly = false; //关闭只读保护
第二种方式,使用response来实例化cookie容器
$cookie = Yii::$app->response->cookies;$cookie->add(new Cookie([ 'name' => 'goods_cart', 'value' => '100']));
导致这个问题的原因是request和response实例化的容器中的readOnly属性的值不同,分别打印的结果如下:
$cookieObj = Yii::$app->request->cookies;var_dump($cookieObj);
$cookie = Yii::$app->response->cookies;var_dump($cookie);exit;
object(yii\web\CookieCollection)#56 (2) { ["readOnly"]=> bool(true) ["_cookies":"yii\web\CookieCollection":private]=> array(1) { ["_csrf-frontend"]=> object(yii\web\Cookie)#63 (7) { ["name"]=> string(14) "_csrf-frontend" ["value"]=> string(32) "JE6d52sN6RdCai-RNDGVYrfpKQ1p0W5_" ["domain"]=> string(0) "" ["expire"]=> NULL ["path"]=> string(1) "/" ["secure"]=> bool(false) ["httpOnly"]=> bool(true) } } }
object(yii\web\CookieCollection)#65 (2) { ["readOnly"]=> bool(false) ["_cookies":"yii\web\CookieCollection":private]=> array(0) { } }
可以看到response默认为true这也是为了cookie安全考虑一个为有加密的值。
本文为魏永强原创作品未经允许不得转载:MarsWill » Invalid Configuration – yii\base\InvalidConfigException The fileinfo PHP extension is not installed.
- Invalid Call – yii\base\InvalidCallException The cookie collection is read only.
- Invalid Configuration – yii\base\InvalidConfigException The fileinfo PHP extension is not installed
- ResultSet is read only .
- Table is 'read only'
- 安装yii2时出错 Invalid Parameter – yii\base\InvalidParamException
- the root filesystem is currently mounted in read-only mode
- 设计: ListView 接口,and the missing read-only interfaces in java collection framework
- yii\base\InvalidConfigException The directory is not writable by the Web process
- add/remove operation is impossible, because the code element 'Cxxx' is read only
- How to solve “add/remove operation is impossible, because the code element 'Cxxx' is read only”
- "add/removeoperation is impossible, because the code element 'Cxxx' is read only" 的解决办法
- add/remove operation is impossible, because the code element 'Cxxx' is read only
- add/remove operation is impossible, because the code element 'Cxxx' is read only
- VS2008 “Add/Remove operation is impossible,becase the code element 'ConnectionAddDlg' is read only”
- add/remove operation is impossible, because the code element 'Cxxx' is read only
- MySql出现#1036 – Table ‘ ‘ is read only 错误解决方法
- 1209 - The MySQL server is running with the--read-only (二)
- The MySQL server is running with the --read-only option so it cannot execute this statement
- fiddler教程
- 两个es集群部署在一台机器上不成功 已解决
- 汉诺塔算法
- 了解Ajax基础
- Ubuntu14.04下安装 Python图表绘制库
- Invalid Call – yii\base\InvalidCallException The cookie collection is read only.
- magento 产品页随机调用同子分类产品
- 十个程序员里面九个闷葫芦???
- 001设计一个有GetMin功能的栈
- FZU1002-HangOver
- 面向对象分析的三个模型与5个层次
- applicationContext-common.xml 之spring tx:advice 和 aop:config 配置事务属性详解
- Sqlserver数据库批量导入数据
- CCF201612-2工资计算