【问题标题】:How to access variables in Helper in CakePHP 3如何在 CakePHP 3 中访问 Helper 中的变量
【发布时间】:2016-09-13 13:01:46
【问题描述】:

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

在 CakePHP 2.x 中这曾经可以工作:

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

【问题讨论】:

  • 在 cake3 中也是如此。

标签: php cakephp cakephp-3.0 helper


【解决方案1】:

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:     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多