在另一个助手中使用Session Helper的问题

时间:2014-05-02 09:45:58

标签: php session cakephp

我正在尝试在另一个Helper中使用Session Helper。

App::uses('AppHelper', 'View/Helper');

/**
 * @alias Mathjax Class for helper
 * @description This class is to define helper in CakePHP. 
 */
class MathjaxHelper extends AppHelper{
    var $helpers = array ('Html', 'Form', 'Session'); 

    var $status = false;

    /*
     * Constructor of the class.
     */
    function __construct(){
            $this->setStatus();
    }

    function create(){
            return $this->status;
    }

    /*
     * mantain the status that the js files are loaded already
     * 
     * When we call help multiple time it may happen that same js files loaded with next helper call.
     * 
     */
     private function setStatus(){
            $this->status = $this->Session->read('Mathjax.status');
            if( $this->Session->check('Mathjax.status') ){
                    $this->status = $this->Session->read('Mathjax.status');
            } else {
                    $this->Session->write('Mathjax.count', $status);
            }
    }
}

但是这里没有使用Session Helper。 CakePHP抛出错误:

Error: Call to a member function read() on a non-object
File: /var/www/PHP/folder/app/View/Helper/MathjaxHelper.php
Line: 31

任何人都可以帮助我吗?我怎么能在另一个助手中使用Session Helper?

1 个答案:

答案 0 :(得分:2)

好孩子打电话给他们的父母

此:

function __construct(){
    $this->setStatus();
}

意味着你的助手不会继承任何the base code for a helper,每当覆盖一个方法时,除非有特殊原因,否则调用父方法是个好主意。

此方法也有不同的方法签名。

因此,请将其更改为:

function __construct(View $View, $settings = array()) {
    parent::__construct($View, $settings);
    $this->setStatus();
}

使用书面形式,或考虑使用beforeRender而不是您的定制代码的构造函数。

SessionHelper :: write不存在

请注意the Session helper has no write method - 要写入您需要使用the CakeSession interface的会话。

相关问题