Announcement

Collapse
No announcement yet.

Coding Tutorial v7 : custom console command

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Coding Tutorial v7 : custom console command

    Replace {myModule} with a name of your module (too in php files ..)

    create file in :
    custom/Espo/Modules/{myModule}/Console/Commands/Whoami.php

    PHP Code:
    <?php
    namespace Espo\Modules\{myModule}\Console\Commands;

    use 
    Espo\Core\Exceptions\Error;
    use 
    Espo\Core\Console\IO;

    class 
    Whoami extends \Espo\Core\Console\Commands\Base
    {
    protected 
    $io;

    public function 
    __construct(IO $io)
    {
    $this->io $io;
    }

    public function 
    run(array $options, array $flagList, array $argumentList) : void
    {
    $name $argumentList[0] ?? null;
    if (
    $name){
    $this->io->writeLine('Your name is : ' .$name);
    }else{
    $this->io->writeLine("Please give your name.");
    $name $this->io->readLine();
    }

    if (!
    $name) {
    throw new 
    Error('Invalid argument. Please give your name');
    }
    $this->io->writeLine('Hello ' .$name);
    $this->io->writeLine("1) Your name is {$name} right?");
    $this->io->writeLine("2) What is my name?" );
    $this->io->writeLine("Select 1 or 2 or return for cancel.");

    $response $this->io->readLine();
    switch (
    $response)
    {
    case 
    :
    $this->io->writeLine("Yes.. no complicate !");
    break;
    case 
    :
    $whoAmi = @exec('whoami');
    $this->io->writeLine($whoAmi);
    break;
    default:
    $this->io->writeLine("Canceled.");
    return;
    }
    $this->io->writeLine("Done.");
    }
    }
    create file in :
    custom/Espo/Modules/{myModule}/Resources/metadata/app/consoleCommands.js

    PHP Code:
    {
    "whoami": {
    "className""\\Espo\\Modules\\{myModule}\\Console\\Commands\\Whoami"
    }


    clearCache

    php bin/command whoami
    php bin/command whoami espoCRM

    This is just for learning
    Last edited by item; 11-08-2021, 09:22 PM.

  • #2
    Excellent, thanks item !

    Comment


    • #3
      Help pleas (((

      Error: Command 'unique-numeric' does not exist.

      Comment


      • #4
        item nice one!
        Maybe i'll record a video with this

        Comment

        Working...
        X