Command execution from Controller

Created custom commands to import the data(Not a shell comand), I want to execute the command from the controller, Let me know how can I execute the command in cakephp4 controller.

I tried to use
use Cake\Console\CommandRunner;

$command = new CommandRunner();
$command->run([‘import_users’]);

But I am getting below error while creating object**($command = new CommandRunner();)**
“Too few arguments to function Cake\Console\CommandRunner::__construct()”

Seems pretty straight-forward. The constructor for that class needs parameters; it needs at least a ConsoleApplicationInterface object.

If the comand spends more time than the maximum request time (usually 30-40 seconds), the executing commands is killed when reaches that limit.

I suggest creating a process with that command, so that the request finishes quickly. The command should be responsible to update process progress somewhere (like having a tasks model and update percent there)

Create a new process like this: (launch ExampleCommand)

exec(
    'sh -c "php -d memory_limit=512M ' . ROOT .
    '/bin/cake.php Example ' . $params .
    ' > /dev/null 2>&1 &"'
);

For more advanced queue plugins you can use:
queue
cakephp-queue

1 Like