In CakePHP, where would I put a method that perfor

2019-08-10 02:42发布

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?

2条回答
smile是对你的礼貌
2楼-- · 2019-08-10 02:57

On AppController

function beforeFilter() 
 {

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

}
查看更多
淡お忘
3楼-- · 2019-08-10 03:01

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

查看更多
登录 后发表回答