CakePHP: How to use/access view variables inside a Helper

In CakePHP 3 application, you can use the your view variables inside a Helper. There are 2 ways to access/use view variables in our Helper.

1. Access view variable using class property like $this->_View->viewVars

You can access all the view variables from $this->_View->viewVars array. It contains all the variables which are set from Controller or any other place. See below example,

 

2. Access view variable using API function like $this->_View->get()

This is the recommended way of accessing view variables inside a Helper. You can use get() method of View object and pass the variable name as an argument. See below example,

 

 

You may also like...