如何在命令 Shell 中创建虚拟控制器传递给组件 CakePHP 2.4.3

How to create a dummy Controller in Command Shell pass to Component CakePHP 2.4.3

我使用 CakePHP 2.4.3

我在命令中的代码ShellPhulyShell.php

<?php
App::uses('ComponentCollection', 'Controller');
App::uses('HopeeComponent', 'Controller/Component');

class PhulyShell extends AppShell {

    public $components = array('Hopee');

    public function initialize() {
        $collection = new ComponentCollection();
        $this->Hopee = new HopeeComponent($collection);
    }
}

HopeeComponent.php之前是别人写的,我无权编辑。 文件里面有一段代码

public function __construct(ComponentCollection $collection, $settings = array()) {
     if($this->_Controller->request != null){
        $shortcut_app_flag = $this->_Controller->request->query('phuly_app');
     }
}

会报错,因为没有controller $this->_Controller

Notice Error: Trying to get property of non-object in [cakephp-2.4.3/phuly/Controller/Component/HopeeComponent.php, line 112]

我知道一种解决方案是将控制器传递给它

<?php
App::uses('ComponentCollection', 'Controller');
App::uses('HopeeComponent', 'Controller/Component');
App::uses('AppController', 'Controller');
App::uses('TestController', 'Controller');

class PhulyShell extends AppShell {

    public $components = array('Hopee');

    public function initialize() {
        $collection = new ComponentCollection();
        $collection->init(new TestController);
        $this->Hopee = new HopeeComponent($collection);
    }
}

有效但没有显示 Notice Error,但我不想创建文件 TestController.php,我无法使用 AppController.php

有没有办法将虚拟控制器传递给组件而不在Shell命令中创建文件控制器?

谢谢大家!

我自己找到了答案,就是用Controller,我傻了没注意到这个

   <?php
    App::uses('ComponentCollection', 'Controller');
    App::uses('HopeeComponent', 'Controller/Component');
    App::uses('Controller', 'Controller');
    
    class PhulyShell extends AppShell {
    
        public $components = array('Hopee');
    
        public function initialize() {
            $collection = new ComponentCollection();
            $collection->init(new Controller);
            $this->Hopee = new HopeeComponent($collection);
        }
    }