Using components in Command (formerly Shell) in CakePHP4.
Using components in Command (formerly Shell) in CakePHP4.
Category:CakePHP4
Pub.date:2020-02-29
I'll describe how to use components in CakePHP4 Command.
In CakePHP3, it was Shell, but in CakePHP4, it is Command.
Using Component in Command in CakePHP4
The following codes
src/Command/ExampleOneCommand.php
<?php
declare(strict_types=1);
namespace App\Command;
use Cake\Console\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Controller\ComponentRegistry; //add
use App\Controller\Component\ExampleCommonComponent; //Components to be used
class ExampleOneCommand extends Command {
public function execute(Arguments $args, ConsoleIo $io) {
$this->ExampleCommon = new ExampleCommonComponent(new ComponentRegistry()); //Create a component instance
$ret = $this->ExampleCommon->first(); //Example) first method call
$this->subFunc();
}
private function subFunc(): void {
//Of course, it can be used for other functions.
$ret = $this->ExampleCommon->second(); //Example) second method call
}
}
Test run in XAMPP
When testing with XAMPP in your local environment, you can do the following in the console
(It may vary depending on the environment)
D:\path\to\bin>php cake.php example_one
Calling in CRON on Linux
The path to call it with CRON on Linux is as follows
/path/to/bin/cake.php example_one
CONTENTS
JavaScript
Unreal Engine
CakePHP4
CakePHP4
Flutter
Flutter
Other
JavaScript
JavaScript
CakePHP4
Web Server
Photoshop
Unreal Engine
CakePHP4
Web Browser
Web Server
iOS
Android
Web Browser
CakePHP4
Plesk
Illustrator
Plesk
Web Server
Web Server
CakePHP3
Web Browser
CakePHP3
JavaScript
JavaScript
CakePHP3
CakePHP3