Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在命令Shell pass to Component CakePHP 2.4.3中创建虚拟控制器_Php_Cakephp_Cakephp 2.4_Cakephp 2.x - Fatal编程技术网

如何在命令Shell pass to Component CakePHP 2.4.3中创建虚拟控制器

如何在命令Shell pass to Component CakePHP 2.4.3中创建虚拟控制器,php,cakephp,cakephp-2.4,cakephp-2.x,Php,Cakephp,Cakephp 2.4,Cakephp 2.x,我使用CakePHP2.4.3 命令Shell PhulyShell.php中的我的代码 <?php App::uses('ComponentCollection', 'Controller'); App::uses('HopeeComponent', 'Controller/Component'); class PhulyShell extends AppShell { public $components = array('Hopee'); public func

我使用CakePHP2.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);
    }
}
它将抛出一个错误,因为没有控制器$this->\u控制器

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

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

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

你真的不应该这样做。告诉那些说你不允许编辑文件的人,他们实施这些限制是错误的,因为这将导致你不得不编写错误的代码。如果您需要在组件和shell命令中使用相同的代码,那么您应该将该代码移动到两个层都可以使用的单独服务中!
   <?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);
        }
    }