San*_*pta 5 components cakephp-2.0
我正在尝试使用cakephp shell为我的应用程序实现一个任务.该任务涉及运行一个长时间运行的进程(因此需要使用shell).
该函数要求我在Component中使用一个名为CommonComponent的函数
不幸的是,每当我尝试包含该组件时,我得到以下错误PHP致命错误:在/var/www/nginx-test/app/Controller/Component/CommonComponent.php中找不到类'组件'
这是被调用的CronShell类
class CronShell extends AppShell {
public function main() {
$this->out('Hello world.');
// $this->out(phpinfo());
}
public function test()
{
$this->out('Before Import');
App::import('Component', 'Common');
$this->out('Import complete');
// $this->Common=ClassRegistry::init('CommonComponent');
$this->Common =new CommonComponent();
$this->out('Initialization complete');
$this->Common->testCron();
$this->out('FunctionCall complete');
//$this->Common->saveCacheEntry("name","value");
}
}
Run Code Online (Sandbox Code Playgroud)
CommonComponent类存储为app/Controller/Component/CommonComponent.php,如下所示
class CommonComponent extends Component
{
function testCron()
{
$this->out('Hello world from Component.');
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Ste*_*ber 15
我最近用我编写的MTurk组件做了这个.我的最终解决方案是使用lib而不是组件.然后我让组件访问lib,这样我就可以使用组件和shell中的方法.
但是,这里的代码允许您从shell加载组件.
<?php
App::uses('AppShell', 'Console/Command');
App::uses('ComponentCollection', 'Controller');
App::uses('Controller', 'Controller');
App::uses('MTurkComponent', 'Controller/Component');
class ProcessCompletedTask extends Shell {
public function execute() {
$this->out("Processing...\n");
$collection = new ComponentCollection();
$this->MTurk = new MTurkComponent($collection);
$controller = new Controller();
$this->MTurk->initialize($controller);
$this->MTurk->processHITs();
$this->out("Complete\n");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 1
导入到 Shell 中的内容应该是应用程序库中的代码
该组件还可以使用 Lib 代码 - 但如果你设置正确,你将不需要做大量繁琐的事情,你会让你的应用程序更干净
如果你导入组件,你需要向它传递一个组件集合,所以你必须从 shell 中创建它,而不是你使用它(或者如果你这样做,你一定做错了)
| 归档时间: |
|
| 查看次数: |
10645 次 |
| 最近记录: |