user3305818
user3305818

Reputation: 184

Issue with Session Helper in another Helper

I am trying to use the Session Helper in another 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);
            }
    }
}

But here the Session Helper is not available to use. CakePHP throws an error as:

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

Can anyone help me? How could I use the Session Helper in another Helper?

Upvotes: 1

Views: 832

Answers (1)

AD7six
AD7six

Reputation: 66299

Good children call their parents

This:

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

Means that your helper inherits none of the base code for a helper, whenever overriding a method, it's a good idea to call the parent method unless there's a specific reason not to.

This method also has a different method signature.

Therefore, change it to:

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

To use as written, or consider using beforeRender and not the constructor for your bespoke code.

SessionHelper::write does not exist

Note that the Session helper has no write method - to write to the session you'll need to use the CakeSession interface.

Upvotes: 2

Related Questions