在自定义组件中使用会话组件

Luk*_*cki 2 cakephp cakephp-2.0 cakephp-2.3

我正在尝试在自定义组件(CakePHP 2.3)中使用Session组件,但是当我调用Session组件函数时,我得到:致命错误:在...\app\Controller \中的非对象上调用成员函数read()第7行的Component\CartComponent.php

我的CartComponent看起来像这样:

<?php
App::uses('Component', 'Controller');
class CartComponent extends Component {
    public $components = array('Session');

    function hasItems() {
        $cart = $this->Session->read('Cart');
        return $cart != null && count($cart) > 0;
    }

}
?>
Run Code Online (Sandbox Code Playgroud)

我在控制器中使用它:

<?php
class OrdersController extends AppController {
    public $name = 'Orders';
    public $components = array('Cart', 'Email');

    function beforeFilter() {
        parent::beforeFilter();
        if ($this->Cart->hasItems()) {
            $this->Auth->allow('add_item', 'remove_item', 'cart');
        } else {
            $this->Auth->allow('add_item', 'remove_item', 'cart', 'make');
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

小智 8

在我尝试使用的自定义组件中使用会话

public $components = array('Session');
Run Code Online (Sandbox Code Playgroud)

然后通过使用来调用它

$this->Session->read('Cart');
Run Code Online (Sandbox Code Playgroud)

但我无法使用它,我开始使用它

CakeSession::read('Cart')
Run Code Online (Sandbox Code Playgroud)

现在它的作品希望它会用于你注意我用在蛋糕php版本> 2