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");
    }
}
New contributor
NoOn is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
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 at 12:09
  • you need to do the logic into the execute method which you want to call. Oct 12 at 12:12
  • ok and then how to do when i call cli command work that function in block file?
    – NoOn
    Oct 12 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 at 12:17
  • Can you write expamle in your answer?(edit it)
    – NoOn
    Oct 12 at 12:19

Your Answer

NoOn is a new contributor. Be nice, and check out our Code of Conduct.

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.