Here is a simple hello world command run using the command
– Please create the following module for command :
app/code/Vendor/Module/registration.php
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Module" setup_version="0.1.0"/> </config>
- Create a new model class, it will contain the options, description, and logic of your command.
app/code/Vendor/Module/Model/Generation.php
namespace VendorModuleModel; use SymfonyComponentConsoleCommandCommand; use SymfonyComponentConsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; class Generation extends Command { protected function configure() { $this->setName('custom:create') ->setDescription('The description of you command here!'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Hello World!'); } }
app/code/Andre/Tools/etc/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="MagentoFrameworkConsoleCommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="create" xsi:type="object">VendorModuleModelGeneration</item> </argument> </arguments> </type> </config>
Now, run upgrade command to register module :
bin/magento setup:upgrade
To run the command, just create :
bin/magento custom:create.
Then add your own logic under the execute() method.
Please let us know if this article is helpful to you.