I have a function in one of my views that formats data coming from the DB before displaying it. Since I use this function in many views, I'd like to make a global function that would be accessible from every view. How would I do that ?
As mentioned in the other answers, creating a helper is probably what you are looking for. See the cookbook entry for more information.
To make your helper available in all your views, add the helper to the $helpers
array of your AppController
(app/Controller/AppController.php
).
Creating a helper (as Headshota and preinheimer explained) is the best idea if the function is complex..
But if your function is simple, you can open the file app/config/bootstrap.php
write your function in this file and that's it..
the function will be accessible anywhere (models, controllers, views, etc)
hope that helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With