在CakePHP中放置自定义函数的位置 [英] Where to put a custom function in CakePHP

查看:258
本文介绍了在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 ?

推荐答案

如其他答案所述,创建助手可能是您正在寻找的。有关详情,请参阅食谱条目

As mentioned in the other answers, creating a helper is probably what you are looking for. See the cookbook entry for more information.

要在所有视图中使用帮助程序,请将帮助程序添加到 AppController $ helpers app / Controller / AppController.php )。

To make your helper available in all your views, add the helper to the $helpers array of your AppController (app/Controller/AppController.php).

这篇关于在CakePHP中放置自定义函数的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆