dq62957 2014-01-21 19:32
浏览 21

CakePHP共享控制器视图/功能

I don't know how to do this:

I have a CakePHP project that uses AdministratorsController and UsersController for different roles, etc.. but, a User can save a Message and a Administrator too! So, should I repeat the same function or what?

function saveMessage(){
    $this->autoRender = false;
    if (!$this->RequestHandler->isPost())
        $this->Session->setFlash('Error 78.', 'flash_custom_danger');
    else{   
        if(!$this->Message->save($this->request->data))
            $this->Session->setFlash('Error 985.', 'flash_custom_success');
        else
            $this->Session->setFlash('Success!', 'flash_custom_success');       

    }
    $this->redirect('/');
}
  • 写回答

2条回答 默认 最新

  • dongwu8653 2014-01-21 19:38
    关注

    Move the function to app_controller, because both your controllers will extend from it.

    In the app_controller:

    function my_function($controller_specific_string = null){ 
        //stuff here common to both controllers
    }
    

    In the controller(s):

    function my_function() { //extended function in both controllers
      // do controller specific stuff here...
    
      return parent::my_function('from_users_controller');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改