简体   繁体   中英

Magento 2: How to run CLI command from another CLI command class?

I'm working on a custom CLI command & I was wondering what's the best way to call other commands from the PHP code (without shell_exec() or similar).
For example:
When running "php bin/magento my:custom:command", it'll do it's thing & in the end will run "php bin/magento cache:flush".

Any Ideas?

Thanks.

The Magento CLI is built on top of Symfony Console. You can load up and run other commands with 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 information here . Although it's unlikely to be a problem for you, please note that the documentation suggests this is not always the best idea:

Most of the times, 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's output is optimized for the console and not to be passed to other commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM