开发者

cakephp accessing view attributes/variables from within a helper

开发者 https://www.devze.com 2023-01-12 02:37 出处:网络
is there a reasonable way to access the view attribute \"passedArgs\" (or any similar) /* view */ $this->passedArgs

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.


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


Have you tried just setting the view's value from the AppController?

class AppController extends Controller {
 function beforeFilter() {
  // other stuff
  $this->set( 'passed_args', $this->params['pass'] );
 }
}


Cake 3:

$this->getView()->get('my_var');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号