问题描述
背景:我有一个方法 levelCheck()
,它将当前用户的级别与几个参数进行比较,并返回true或false。我需要能够从任何控制器访问此方法,我也想调用它在帮助程序中用于菜单等。
Background: I have a method levelCheck()
which compares the current user's level to a few parameters, and returns a true or false. I need to be able to access this method from any controller, and I would also like to put a call to it inside a helper for use on menus, etc.
问题:由于Cake的灵活性,我几乎可以从任何地方调用Cake。哪里是正确的地方放这个?在自定义会话(扩展)?在AppController中?一个新的组件处理当前用户?在用户模型或用户控制器?
Question: Due to Cake's flexibility, I can call almost anything from almost anywhere with Cake. Where is the correct place to put this? In a custom Session (extended)? In the AppController? A new component dealing with the current user? In the UserModel or User Controller?
这里的重要一点是我(或其他人)将来如何确定这样的事情的正确位置?
The important piece here is how would I (or others) determine the correct location for such a thing in the future?
推荐答案
将此方法放在AppController中
Put this method in AppController
class AppController extends Controller
{
function levelCheck(){
# whatever
}
}
这是此操作的正确位置。因为AppController在所有控制器中扩展,所以这个方法可以使用当前控制器对象$ this-> levelCheck()来调用。
This is the correct place of this action. Because AppController is extended in all the controller so this method can called using current controller object that is $this->levelCheck().
这篇关于在CakePHP中,我将在哪里对会话用户执行检查的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!