0

How can call function from block to cli command magento 2 here is my cli command class

class Hicustomer extends Command
{
 

    protected function configure()
    {
        $this->setName('cloudways:hicustomer');
        $this->setDescription('Demo command line');
        parent::configure();
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        
       //call function from block
        $output->writeln("Hi Customer");
    }
}

1 Answer 1

0

You need to run this command :

php bin/magento cloudways:hicustomer

To call block file you need to do like this :

class Hicustomer extends Command
{
 
    protected $myblockobject;

    public function __construct(\Vendor\Extension\Block\Mycustomblock $myblockobject)
    {
       $this->myblockobject = $myblockobject;
       parent::__construct();
    }

    protected function configure()
    {
        $this->setName('cloudways:hicustomer');
        $this->setDescription('Demo command line');
        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        
       $myblockobject = $this->myblockobject->create()->MyCustomFunction();

       //call function from block
        $output->writeln("Hi Customer");
    }
}
9
  • yes! when I run this command how can I do for run my block function? This is real?
    – NoOn
    Oct 12, 2021 at 12:09
  • you need to do the logic into the execute method which you want to call. Oct 12, 2021 at 12:12
  • ok and then how to do when i call cli command work that function in block file?
    – NoOn
    Oct 12, 2021 at 12:13
  • if you want to call the your block file function then you can declare that into constructore and then use that object when you want. Oct 12, 2021 at 12:17
  • Can you write expamle in your answer?(edit it)
    – NoOn
    Oct 12, 2021 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.