我正在尝试在自定义组件(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;
}
}
?>
我在控制器中使用它:
<?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');
}
}
}
?>