Magento 2: How to run a CLI command from another CLI command class?
I'm working on a custom CLI command and I was wondering what is the best way to call other commands from PHP code (without shell_exec () or similar).
For example:
When you run "php bin / magento my: custom: command" it will do this and at the end it will run "php bin / magento cache: flush".
Any ideas?
Thank.
source to share
The Magento CLI is built on top of the Symfony Console. You can download and run other commands using this component as such:
$arguments = new ArrayInput(['command' => 'my:custom:command']);
$this->getApplication()->find('my:custom:command')->run($arguments, $output);
$arguments = new ArrayInput(['command' => 'cache:flush']);
$this->getApplication()->find('cache:flush')->run($arguments, $output);
More details here . While this is unlikely to be a problem for you, please note that the documentation suggests that this is not always the best idea:
In most cases, calling a command from code that is not executed on the command line is not a good idea. The main reason is that the command output is optimized for the console and is not passed on to other commands.
source to share