PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] How to access variables in Helper in CakePHP 3

 January 23, 2022     cakephp, cakephp-3.0, helper, php     No comments   

Issue

How can I access the variables which are set to the view in CakePHP 3 in a Helper? I did not find anything in the documentation.

In CakePHP 2.x this used to work:

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


Solution

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


Answered By - floriank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing