While using Magento 2, you are probably familiar with tons commands and executed multiple times. By default, Magento comes with preloaded SSH features that allows you to securely log into remote server or system and execute commands. Basically, SSH was designed to provide secure communications between two untrusted networks. But have you ever wondered where how these commands were created and how to create your own command?
In today’s blog, I will show you how to create your own SSH command in Magento 2 with simple steps. We need to create two files to override default Magento files using a custom Magento 2 extension. Here, I have demonstrated simple SSH command to see extension is enabled or not?!?!
Firstly, We need to Point Command List Class in ‘di.xml‘
Code :
app\code\Vendor\Extension\etc\di.xml
1 2 3 4 5 6 |
<!--?xml version="1.0"?--> Vendor\Extension \Console\Command\Cod |
app\code\Vendor\Extension\console\command\enable.php
1 2 3 |
<!--?php namespace Vendor\Extension\Console\Command; class Cod extends \Symfony\Component\Console\Command\Command { protected $scopeConfig; public function __construct( \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->orderCollectionFactory = $orderCollectionFactory; $this->scopeConfig = $scopeConfig; parent::__construct(); } protected function execute( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output ) { $output->writeln('Extensions checking Start...'); if($this->scopeConfig->getValue('section/group/field', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) { $output->writeln('Extension is Enable'); } else { $output->writeln('Extension is Disable'); } $output->writeln('Extensions checking finished.'); } protected function configure() { $this->setName("extension:enable"); $this->setDescription("Check Extension is enable or not "); $this->setDefinition([ ]); parent::configure(); } } </pre> <p>Taada, Command is created! You can also create one or more custom SSH command using this way as per your requirements.<br ?--> Don’t forget to tell us, how you have used this code to create your own command. If you are looking for more help regarding this code kindly comment down below. Happy SSH! |