How to load a component in the console / shell

In CakePHP, how do I load a component into a shell?

To use a component in the controller, you include an array of components in a property called $ components. This does not work for my Shell. The documentation also does not indicate loading "on the fly."

class MakePdfsShell extends Shell {
    public $components = array('Document'); // <- this doesn't work
    public function main()
    {
        $this->Document = $this->Components->load('Document'); // <- this doesnt work either
        $this->Document->generate(); // <- this is what I want to do
    }
    ...
+4
source share
5 answers

If you are trying to access a custom XyzComponent from a shell, you probably have public functionality. The right place for publicly accessible functionality (also accessible from shells) is at /Lib/.

XyzComponent /Controller/Component/XyzComponent.php /Lib/Xyz/Xyz.php. ( , "Component", , "XyzComponent" "Xyz".)

, "Xyz" class::$components.

App::uses('Xyz', 'Xyz'); // that ('ClassName', 'folder_under_/Lib/')

. $this->Xyz = new Xyz(); , .

+3

xml, . , PHP CLI, ( , ).

xml , , . Lib , CakePhp v3, , Lib.

, , . :

namespace App\Shell;

use Cake\Console\Shell;

use Cake\Core\App;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use App\Controller\Component\XmlUtilitiesComponent;   // <- resides in your app src/Controller/Component folder

class XmlCheckShell extends Shell
{

    public function initialize() {
        $this->Utilities = new XmlUtilitiesComponent(new ComponentRegistry());
    }

...

$this->Utilities .

+10

.

, , .

, . . , HTTP- , .

XML ? . , , App\Utility\XmlUtils , , no .

, . , XML, ( ) , .

.

, Google SO :

, . .

+7

, YourComponent:

<?php

App::uses('Component', 'Controller');

class YourComponent extends Component {

    public function testMe() {
        return 'success';
    }
}

- 2.,

App::uses('ComponentCollection', 'Controller');
App::uses('YourComponent', 'Controller/Component');

class YourShell extends AppShell {

    public function startup() {
        $collection = new ComponentCollection();
        $this->yourComponent = $collection->load('Your');
    }

    public function main() {
        $this->yourComponent->testMe();
    }
}

- 3.

<?php

namespace App\Shell;


use App\Controller\Component\YourComponent;
use Cake\Console\Shell;
use Cake\Controller\ComponentRegistry;

class YourShell extends Shell {

    public function initialize() {
        parent::initialize();
        $this->yourComponent = new YourComponent(new ComponentRegistry(), []);
    }

    public function main() {
        $pages = $this->yourComponent->testMe();
    }
}
+1

//TestShell.php

class TestShell extends AppShell{

    public function test(){
        //to load a component in dis function
     App::import('Component', 'CsvImporter');
     $CsvImporter = new CsvImporterComponent();
     $data = $CsvImporter->get();
    }

}

//CsvImporterComponent.php

App::uses('Component', 'Controller');

class CsvImporterComponent extends Component {

    function get(){
    //your code
    }

}
-3
source

All Articles