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

f0brbegy  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(131)

我使用的是CakePHP 2.4.3
我在命令外壳PhulyShell.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');
     }
}

它将抛出错误,因为没有控制器**$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);
    }
}

它工作并且没有显示通知错误,但是我不想创建一个文件TestController.php,我不能使用AppController.php
有没有办法在Shell命令中不创建文件控制器的情况下将一个虚拟控制器传递给组件?
谢谢大家!

vsaztqbk

vsaztqbk1#

我自己找到了答案,只是使用控制器,我是愚蠢的,没有注意到这一点

<?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);
        }
    }

相关问题