1
votes

MY first time creating a custom helper. I'm getting an error in my Controller Code that I"m calling a a method on a non existed object (helper). Yet i believe my helper "BM" is being loaded successfully because i'm not getting any errors on loading helpers.

Error: Call to a member function mcpGetActiveMerchantID() on a non-object   
File: C:\wamp\www\bm\app\Controller\MCPController.php   
Line: 412

I have placed BMHelper.php into my View\Helper\ directory.

<?php
class BMHelper extends AppHelper{   
    public function mcpGetActiveMerchant(){
        return $this->Session->read('Auth.ActiveMerchant');
    }
    public function mcpGetActiveMerchantID() {
        $activemerchant = $this->Session->read('Auth.ActiveMerchant');
        foreach($activemerchant as $key => $value) {
            $merchant_id = $key; 
        }
        return $merchant_id;
    }
}

?>

Then in my Controller I have this:

<?php
class MCPController extends AppController {

    public $helpers = array('Html', 'Form', 'Session','BM','Js' => array('Jquery'));

    public function walkinadd() {

        $test = $this->BM->mcpGetActiveMerchantID(); //Line 412

    }
}
?>

HEre is the error again (same as the error I pasted at the top)

Error: Call to a member function mcpGetActiveMerchantID() on a non-object   
File: C:\wamp\www\bm\app\Controller\MCPController.php   
Line: 412

Anyone know what is wrong?

1

1 Answers

1
votes

Helpers are to be used in Views not Controllers, though you could do:

public function walkinadd() {
    $view = new View($this);
    $bm = $view->loadHelper('BM');
    $test = $bm->mcpGetActiveMerchantID();
}