php CakePHP:在非对象上调用成员函数 setFlash()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4876791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:09:37  来源:igfitidea点击:

CakePHP: Call to a member function setFlash() on a non-object

phpcakephp

提问by Cameron

I get the following error when trying to logout of my CakePHP app:

尝试注销我的 CakePHP 应用程序时出现以下错误:

Notice (8): Undefined property: UsersController::$Session [APP/controllers/users_controller.php, line 75]
Fatal error: Call to a member function setFlash() on a non-object in /Users/cameron/Sites/cakeapp/app/controllers/users_controller.php on line 75

This is the code for lines 74, 75 and 76:

这是第 74、75 和 76 行的代码:

function logout() {
    $this->Session->setFlash('Good-Bye');
    $this->redirect($this->Auth->logout());
}

回答by mtnorthrop

It looks like you don't have the Session component loaded in your Users controller.

看起来您的用户控制器中没有加载 Session 组件。

The Session component should be loaded by default, but if you've set the components array in AppController this will overwrite the defaults.

默认情况下应该加载 Session 组件,但是如果您在 AppController 中设置了 components 数组,这将覆盖默认值。

This means that if you have

这意味着如果你有

var $components = array();

in your AppController, make sure the Session component is included there:

在您的 AppController 中,确保包含 Session 组件:

var $components = array('Session');

Alternatively, you can load the Session component in your Users controller if you don't want to use it app-wide.

或者,如果您不想在应用程序范围内使用它,您可以在用户控制器中加载 Session 组件。