Skip to content

Commit 1368d46

Browse files
committed
Add details to the cli command development chapter
1 parent 8494808 commit 1368d46

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

developing-extensions-in-magento-2.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,53 @@ of your extension.
209209
Magento 2 allows you to create custom CLI commands that can be executed from the command line. To create a CLI command,
210210
follow these steps:
211211

212-
1. Create a PHP class that extends the `Command` class from the `Symfony\Component\Console` namespace.
213-
2. Implement the necessary methods, including `configure` and `execute`, which define the command's configuration and
214-
behavior.
215-
3. Register your command in the `di.xml` file of your extension.
212+
### 1. Create a PHP class
213+
214+
The PHP class extends the `Command` class from the `Symfony\Component\Console` namespace.
215+
216+
Implement the necessary methods, including `configure` and `execute`, which define the command's configuration and
217+
behavior.
218+
219+
Example:
220+
221+
```php
222+
<?php
223+
namespace MyVendor\MyExtension\Console\Command;
224+
225+
use Symfony\Component\Console\Command\Command;
226+
227+
class MyCommand extends Command
228+
{
229+
protected function configure()
230+
{
231+
$this->setName('myextension:mycommand')
232+
->setDescription('My custom CLI command');
233+
}
234+
235+
protected function execute(InputInterface $input, OutputInterface $output)
236+
{
237+
// Custom command logic goes here
238+
}
239+
}
240+
```
241+
242+
### 2. Register your command in the `di.xml` file of your extension.
243+
244+
Regular commands are registered in the `Magento\Framework\Console\CommandListInterface` class.
245+
246+
Example:
247+
248+
```xml
249+
<!-- file: di.xml -->
250+
<type name="Magento\Framework\Console\CommandListInterface">
251+
<arguments>
252+
<argument name="commands" xsi:type="array">
253+
<item name="myextension_mycommand" xsi:type="object">MyVendor\MyExtension\Console\Command\MyCommand</item>
254+
</argument>
255+
</arguments>
256+
</type>
257+
```
258+
216259

217260
## 8. Packaging and Installing Extensions <a name="packaging-and-installing-extensions"></a>
218261

0 commit comments

Comments
 (0)