Where to put a custom function in CakePHP

11,778

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...

Share:
11,778

Related videos on Youtube

Alex
Author by

Alex

Updated on June 04, 2022

Comments

  • Alex
    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 ?

Related