如何在命令Shell pass to Component CakePHP 2.4.3中创建虚拟控制器
我使用CakePHP2.4.3 命令Shell PhulyShell.php中的我的代码如何在命令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
<?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);
}
}