tags:

views:

3058

answers:

3

I'd like to create a global variable in CakePHP. If I define something in in my app_controller.php like

var $varName

I can access

$this->varName

from any of my controllers, but I cannot get to it from models.

How can I create a global variable accessible from the models?

The value of $varName isn't known until runtime, so I don't think bootstrap.php is an option.

+7  A: 

CakePHP’s new Configure class can be used to store and retrieve application or runtime specific values. Be careful, this class allows you to store anything in it, then use it in any other part of your code

ax
A: 

Actually, why are you doing that? The model should have no idea that a controller exists.

If it is a business (model) object, create it as such. If not, the model should not know it.

Some global variables are pretty standard. For example, my test server and my production server are located at two very different domains, and I often need to create links to them... even in the models. So having a global variable for that doesn't really break any design principles. It actually could potentially save you from breaking design principles.
just_wes
A: 

Global functions and variables for your app can be defined in the app/config/bootstrap.php

for more info see http://book.cakephp.org/view/48/Bootstrapping-CakePHP

Alexander Morland