CakePHP - dynamically changes the core.php variable 'imageBaseUrl'

advertisements

In CakePHP app, I have a model Acccount. I want each account to have its own subfolder in the webroot/img folder. I can create the subfolders no problem.

The challenge is universally identifying which is the current default path. I can get a account id from the Auth.User.account_id.

Rather than modify the img path in every View where it is required by introducing the Auth.User.account_id, I thought it would be better to dynamically change the default image path by reading a value in core.php file and change the 'imageBaseUrl' there so it is available throughout the app.

However, I am haven't found any documentation on how to bring variables into the core.php file. I understand it is a file meant to deliver variables to the app so perhaps it isn't possible.

I have tried to deliver Configure::write value as shown below, but that doesn't appear to be read in the core.php file.

* Web path to the public images directory under webroot.
* If not set defaults to 'img/'
Configure::write('App.imageBaseUrl', 'img/'.Configure::write('imageurl').'/');

Is there some way to bring a variable into core.php file, or perhaps there is another method that can dynamically change the image path dependent on application variable?


You can change the value for App.imageBaseUrl as required in your AppController's beforeRender() callback. No need to change it before that point since the value is only required in views. Plus you will have the session component available.