Using Components in Cakephp 2+ Shell

I am trying to implement a task using cakephp shell for my application. The task is to start a long process (hence the need to use a shell).

The function requires me to use the function inside the CommonComponent Component

Unfortunately, when I try to enable a component, I get the following PHP error Fatal error: Class 'Component' not found in / var / www / nginx -test / app / Controller / Component / CommonComponent.php

Here is the CronShell class called

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

The CommonComponent class is stored as app / Controller / Component / CommonComponent.php and looks like this

 class CommonComponent extends Component
{
 function testCron()
    {    
     $this->out('Hello world from Component.');
    }
 }

Any ideas?

+5
source share
4 answers

, , Lib Lib

Lib, ,

, , witin shell , ( , )

0

MTurk, . lib . lib, , .

, , .

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

App::uses('Component', 'Controller'); , ev. CommonComponent? , , , , $this->Controller->Components->load('CommonComponent'), Controller.

0

, .

, , , .

, , , .

0

All Articles