In a CakePHP application we could define an intermediate abstraction of a controller called AppController. The concept is having a Controller class defined by Cake core, writing controllers for our project and defining app_controller.php file for our common functions repeated in every controller.
In this case we usually use beforeFilter or beforeRender method in AppController in order to define some data to be transferred automatically in every controller. More about app_controller and callback functions.
When a variable is set in beforeFilter, it could be used with it's name later in CakePHP views. For example:
Any view later:
But that variable could not be accessed directly in a controller function. In this case we could use viewVars array in order to access beforeFilter set variable in other controller method, which solves our problem:
function test() { $products = $this->viewVars['products']; $product_1 = $products['P1']; }