13

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.

3 Answers 3

30

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

Sign up to request clarification or add additional context in comments.

4 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.
3

Cake 3:

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.