繁体   English   中英

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

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

我使用 CakePHP 2.4.3

我在命令 Shell 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');
     }
}

它将抛出错误,因为没有 controller $this->_Controller

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

我知道一种解决方案是将 controller 传递给它

<?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 命令中创建文件 Controller 的情况下将虚拟 Controller 传递给组件?

感谢大家!

我自己找到了答案,只用了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);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM