Where to put a custom function in CakePHP
Solution 1
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
).
Solution 2
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...
Related videos on Youtube
Alex
Updated on June 04, 2022Comments
-
Alex almost 2 years
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 ?