简体   繁体   English

CakePHP自定义助手不起作用

[英]CakePHP Custom Helper not working

MY first time creating a custom helper. 我第一次创建自定义助手。 I'm getting an error in my Controller Code that I"m calling aa 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. 我在控制器代码中遇到一个错误,该错误是我在一个不存在的对象(帮助程序)上调用aa方法。但是我相信我的帮助程序“ BM”已成功加载,因为在加载帮助程序时没有任何错误。

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. 我已将BMHelper.php放入View \\ Helper \\目录中。

<?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? 有人知道怎么了吗?

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM