Using custom helper in command

I want to use a custom helper in a custom command. From :

https://book.cakephp.org/4/en/console-commands/input-output.html#command-helpers

it appears I need to :

  1. create my helper in src/command/helper instead of view//helper
  2. Call it with :

$this->helper(‘MyHelper’)->someFunction(“test”);

However when I do this I get:

Call to undefined method App\Command\myCommand::helper()

Did I misunderstand something? It seems that helper is not defined?

Thanks!

I haven’t used this functionality, but I wonder if there’s an issue in the docs. The large majority of examples on the page you linked use $io->helper(), so it would seem to be a function of the ConsoleIo class, not the command class. $this->helper() maybe a typo.

As Zuluru said, apparently the right way to call a command helper is this way:

$io->helper('Heading')->output(['It works!']);

imagen

Were test is the name of the command and Heading the name of the command helper

I have tested this and it works this way