cakephp accessing view attributes/variables from within a helper

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

Class View, View interacts with Helpers and view variables passed in from the controller to render the results of the Provides access to attributes that were deprecated. Cake\View\Helper\FormHelper uses Cake\View its name and the variables that can be found in the view template button() to create buttons inside opened forms.

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'] );
 }
}

Controllers - 1.2, The next most often used controller attributes tell CakePHP what helpers, components, Once you've used set() , the variable can be accessed in your view. View interacts with Helpers and view variables passed in from the controller to render the results of the controller action. Often this is HTML, but can also take the form of JSON, XML, PDF's or streaming files. CakePHP uses a two-step-view pattern. This means that the template content is rendered first, and then inserted into the selected layout.

Cake 3:

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

Class Helper, Abstract base class for all other Helpers in CakePHP. Provides common Location: Cake/View/Helper.php helpers. Provides access to deprecated request properties as well. If the value is not one of these 3, the parameter is not output. This chapter will show you how to create your own helpers, and outline the basic tasks CakePHP’s core helpers can help you accomplish. CakePHP features a number of helpers that aid in view creation. They assist in creating well-formed markup (including forms), aid in formatting text, times and numbers, and can even speed up AJAX functionality

Class View, View interacts with Helpers and view variables passed in from the controller to render the results of the Provides access to attributes that were deprecated. A few attributes exist in CakePHP controllers that give you control over how your view is set inside of a layout. This variable is used to provide access to

Class Cake\View\View, View interacts with Helpers and view variables passed in from the controller to render the results of the controller action. Often this Retrieve the current view type See View::$_passedVars for list of options which get set as class properties. Cake\View\Helper implements Cake\Event\EventListenerInterface uses Cake\Core\InstanceConfigTrait Cake\View\Helper\HtmlHelper uses Cake\View\StringTemplateTrait Namespace: Cake \ View \ Helper

Controllers, models and views are stored in pre-defined directories within CakePHP’s directory structure. Here’s the directory structure that’s used: app/ config/ controllers/ models

Comments
  • For me I needed to get the view variables set from the controller, so it was: $this->viewVars = ClassRegistry::getObject('view')->viewVars; This is then an array, the keys of which being the name of the variables which are extracted and used in the view during render.
  • In CakePHP 2.x, you can access the viewVars from a helper's method using: $this->_View->viewVars['var']
  • +1 for atomicguava, that works whereas ClassRegistry doesn't for me. (Cake 2.3 stable)
  • I opted to put this on the AppHelper::__construct() to make it available for all our helpers.