Pages

Monday, May 23, 2016

Cakephp 2.x site wide view variables

This trick is for old cakephp 2.x versions.

There may have many elegant ways, but I discover it simple and easy for me.

In app/Config/core.php
I put some variables used in multiple places site wide e.g. title, logo or something else like this:


Configure::write('View.title', 'Test Title');
Configure::write('View.logo', 'logo.png');


From the view ( .ctp), I called them like:

<title>
 <?php echo Configure::read('View.title'), ' :', $title_for_layout; ?>
</title>
I can use Configure.read in multiple .ctp files and if I need to change any variable value, I shall just do it in app/Config/core.php and done even if 100 .ctp files have used it.

It is a good software engineering practice.

If any specific .ctp needs to have different value, well option is always open to customize that single one keeping rest of 99 files reading the common Config value.

No comments :

Post a Comment