cakephp accessing view attributes/variables from within a helper

zeroasterisk picture zeroasterisk · Aug 19, 2010 · Viewed 10.3k times · Source

is there a reasonable way to access the view attribute "passedArgs" (or any similar)

/* view */
$this->passedArgs

from within a Helper?

I'd be happy to customize the _construct() of the helper or to customize the app_helper... but I don't want to have to pass $this->passedArgs into the helper on every view or usage.

Answer

Nick picture Nick · Aug 19, 2010

Cake 2.x and 3.x

You can look up your variables in the _View object:

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

Cake 1.x

If you grab the current view object from within the helper you should be able to get to its passedArgs.

class SomeHelper extends AppHelper {
  function __construct($settings = array()){
    $this->passedArgs = ClassRegistry::getObject('view')->passedArgs;
  }
}

Cake 1.2.x

If you grab the current view object from within the helper you should be able to get to its viewVars.

class SomeHelper extends AppHelper {
  function __construct($settings = array()){
    $this->viewVars = ClassRegistry::getObject('view')->viewVars;
  }
}

Enjoy, Nick