【问题标题】:how to use App controller method in another controller cakephp 1.3如何在另一个控制器 cakephp 1.3 中使用 App 控制器方法
【发布时间】:2019-03-03 01:06:46
【问题描述】:

这是蛋糕 php 1.3

我在 appcontroller 中有一个名为 setLog() 的方法。我正在尝试将此方法用作所有控制器的通用方法。但是当在另一个控制器(委员会控制器)中调用它时,它会说

调用未定义的方法 CommitteesController::setLog()

我需要知道如何正确调用这个函数。

应用控制器

App::import('Controller', 'ActionLogs');
  class AppController extends Controller {

 public function setLog($action,$remark,$status){

    $logs = new ActionLogsController;
    $logs->constructClasses();
    $data = ['action'=>$action,'user'=>$this->Auth->user('id'),'remark'=>$remark,'status'=>$status];
    $logs->add($data);
}

}

委员会控制器

function add() {
    if (!empty($this->data)) {
        $this->Committee->create();
        if ($this->Committee->save($this->data)) {
            //create a log for success
            $this->setLog('add new committee',json_encode($this->data),1);

            $this->Session->setFlash(__('The committee has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The committee could not be saved. Please, try again.', true));
            //create a log for failed
            $this->setLog('add new committee',json_encode($this->data),0);
        }
    }
    $committeeTypes = $this->Committee->CommitteeType->find('list');

    $cond = array('group_id' => 2,'is_active' => '1');
    $cond2 = array('group_id' => 4,'is_active' => '1');

    $secretaryUserids = $this->Committee->SecretaryUserid->find('list', array('conditions' => $cond));
    $assistantSecretaryUserids = $this->Committee->AssistantSecretaryUserid->find('list', array('conditions' => $cond2));
    $this->set(compact('committeeTypes', 'secretaryUserids', 'assistantSecretaryUserids'));
}

【问题讨论】:

  • 你应该会找到你的答案here
  • 我已经完成了,在另一个控制器中调用方法。 cakephp 书说:“在 AppController 中创建的控制器属性和方法将可用于所有应用程序的控制器。它是创建所有控制器通用代码的理想场所。” .. 但我需要了解应用控制器

标签: php cakephp-1.3


【解决方案1】:

您可以选择以下两种之一:

继承:创建一个BaseController,你可以在其中放置控制器共享的所有方法:

class BaseController extends Controller
{
      public function setLog() 
      {
           // Code here
      }
}

class YourController extends BaseController 
{
     public function yourControllerFunction()
     {
         $this->setLog();
     } 
}

setLog() 必须是公共的或受保护的,而不是私有的,否则它只能在 BaseController 中工作。

可能你的AppController就是我上面所说的BaseController,所以你要做的很简单

class YourController extends AppController

您将能够使用setLog() 函数。

另一种共享函数的方式是使用 Traits:

trait LoggerTrait
{
    public function setLog()
    {
        // Code here
    }
}

class YourController extends Controller
{
    use LoggerTrait;

    public function yourControllerFunction()
    {
        $this->setLog();
    } 
}

【讨论】:

  • 是的,我做了你在第一个块中解释的内容.. 只是一个在基本控制器 (Appcontroller) 中回显某些内容的公共函数,然后我在子控制器中调用它。事情是它说调用未定义的方法 CommitteesController::test()。
  • 您的 CommiteeController 是否在类声明中扩展了 AppController?仅需要/导入是不够的
  • YES,委员会控制器类扩展 AppController {
猜你喜欢
  • 2012-04-24
  • 2015-06-20
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多