访问 View cakePHP 中的帮助程序选项


access helpers options in view cakePHP

假设在我的app_controller.php中,我包含一个这样的帮助程序:

var $helpers = array('MyHelper' => array('value' => 'This is a value'));    

正如您在上面看到的,我还为我的助手设置了一个数组。如何在视图文件中访问此数组?有人知道我该怎么做吗?

在您看来:如果您尝试像访问函数一样访问选项成员变量会发生什么?

<?php
    ...
    ...
    debug( $this->MyHelper->options['value'] );
    // or
    debug( $this->MyHelper->options );
    // to view the whole array - access them by key like above
    ...
?>

与其投反对票,不如阅读@thecodeparadox明确解决你的问题时给你的答案。

可以

通过以下方式访问:

$helpers["Myhelper"]["value"];
class MyHelper extends AppHelper {
    public $options = array();
    public function __construct(View $view, $option = array()) {
        parent::__construct($view, $option);
        $this->options = $option;
        debug($option); // with in $option you will get your array
    }
}
class AppController extends Controller {
    public $helpers = array('MyHelper' => array('option1' => 'value1'));
}

然后在您的视图文件中尝试

$this->MyHelper->options;