In CakePHP, where would I put a method that performs a check on the Session User?

久未见 提交于 2019-12-11 13:54:15

问题


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.


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?


回答1:


Put this method in AppController

class AppController extends Controller 
{

  function levelCheck(){
    # whatever
  }

}

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




回答2:


On AppController

function beforeFilter() 
 {

      $this->custome_componnetst_name->levelCheck(parameters.....); 
      /*action*/

}


来源:https://stackoverflow.com/questions/21493623/in-cakephp-where-would-i-put-a-method-that-performs-a-check-on-the-session-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!