如何在CakePHP 3中访问Helper中的变量

时间:2016-09-13 13:01:47

标签: php cakephp cakephp-3.0 helper

如何在Helper中访问CakePHP 3中为视图设置的变量?我没有在文档中找到任何内容。

在CakePHP 2.x中,这曾经起作用:

$this->_View->viewVars['foo'];

1 个答案:

答案 0 :(得分:3)

Reading the API helps.

 655:     /**
 656:      * Returns the contents of the given View variable.
 657:      *
 658:      * @param string $var The view var you want the contents of.
 659:      * @param mixed $default The default/fallback content of $var.
 660:      * @return mixed The content of the named var if its set, otherwise $default.
 661:      */
 662:     public function get($var, $default = null)
 663:     {
 664:         if (!isset($this->viewVars[$var])) {
 665:             return $default;
 666:         }
 667: 
 668:         return $this->viewVars[$var];
 669:     }