Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put a custom function in CakePHP

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 ?

like image 678
Alex Avatar asked Apr 03 '11 15:04

Alex


2 Answers

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

like image 135
dhofstet Avatar answered Oct 14 '22 23:10

dhofstet


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

like image 28
Mouad Debbar Avatar answered Oct 14 '22 23:10

Mouad Debbar