【问题标题】:cakephp3 session deprecatedcakephp3 会话已弃用
【发布时间】:2015-12-16 14:10:53
【问题描述】:

我已将最新版本的 cakephp 核心更新到我的项目中。 现在我得到这个错误:

Deprecated (16384): SessionHelper has been deprecated. Use request->session() instead. [CORE/src/View/Helper/sessionHelper.php, line 39]

我认为的错误在这一行:

if ($this->session->read('admin_logged_in')){

我应该改用什么?

谢谢

【问题讨论】:

  • 不想做个聪明人,但错误信息告诉你,不是吗? "...改用request->session()..."
  • 消息告诉你! “改用 request->session()。”
  • 我使用过 $this->request->session->read 但返回错误:在非对象 @ndm 上调用成员函数 read()

标签: php session cakephp cakephp-3.1


【解决方案1】:
if ($this->request->session->read('admin_logged_in')){

应该没问题。

更新

根据@raph 下面的评论,正确答案是

$this->request->session()->read('admin_logged_in')

注意会话后的 ()

【讨论】:

  • 返回错误:在非对象上调用成员函数 read()
  • 你在哪里称呼它?在控制器中?
  • if ($this->request->session()->read('admin_logged_in')){ - “session()” 是一种方法,而不是一种属性 - 我的错误 :)跨度>
  • ... 但是,在 3.6 中,显示此消息:已弃用 (16384):ServerRequest::session() 已弃用。请改用 getSession()。 setter 部分将被删除。...更新到:$this->request->getSession()->read('admin_logged_in')
  • 以上答案对于 cakephp 3.6 及之前版本是正确的。在 Cakephp 3.7 中, session() 更改为 getSession
【解决方案2】:

在 Cakehp 3.7 中,改用 getSession()。

$this->request->getSession()->read('admin_logged_in');

参考:https://book.cakephp.org/3.0/en/development/sessions.html

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 2015-06-15
    • 2012-02-12
    • 2014-02-12
    • 2022-07-16
    • 1970-01-01
    • 2015-02-10
    • 2016-03-03
    相关资源
    最近更新 更多