In Spryker, developers can create custom Symfony console commands to automate tasks, provide backend services, or trigger specific business logic directly from the command line. This blog post will guide you through the process of creating a simple custom console command in a Spryker module, using the BluethinkInc:Showmessage command as an example.
What You’ll Learn
- How to create a custom Symfony console command in a Spryker module.
- How to configure the command and display output to the console.
- How to register and execute the new command.
Prerequisites
Before we begin, make sure that you have:
- A working Spryker environment set up.
- Basic knowledge of how Spryker modules and Symfony console commands work.
- To get some basics please follow blogs:
- Install Spryker
- Custom Module in Spyker
Step 1: Create a New Console Command Class
First, we’ll create a new console command inside the
your-spryker-project/src/Pyz/Zed/BluethinkInc/Communication/Console/BluethinkIncConsole.php
To create the command, we need to extend Spryker’s Console class (which is a wrapper around Symfony’s Command class). Let’s create a file BluethinkIncConsole.php inside the Communication\Console directory of your Spryker module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php namespace Pyz\Zed\BluethinkInc\Communication\Console; use Spryker\Zed\Kernel\Communication\Console\Console; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; /** * @method \Pyz\Zed\BluethinkInc\Business\BluethinkIncFacade getFacade() */ class BluethinkIncConsole extends Console { const COMMAND_NAME = 'BluethinkInc:Showmessage'; const DESCRIPTION = 'Displays a custom message!'; /** * @return void */ protected function configure() { $this->setName(static::COMMAND_NAME) ->setDescription(static::DESCRIPTION); } /** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * * @return int */ protected function execute(InputInterface $input, OutputInterface $output): int { // Display the message $message = 'Hello, this is a custom message from BluethinkInc:Showmessage!'; $output->writeln($message); return static::CODE_SUCCESS; } } |
Step 2: Breakdown of the Code
Let’s break down the key parts of the code:
- Command Name and Description:
- We define the constant COMMAND_NAME as BluethinkInc:Showmessage. This is the name you will use to execute the command in the terminal.
- DESCRIPTION gives a brief description of what the command does.
- Configure Method:
- In the configure() method, we set the name and description for our command using $this->setName() and $this->setDescription().
- Execute Method:
- The execute() method is the core of the command. This is where we define the logic that should be run when the command is executed.
- In our case, we use $output->writeln() to display a message to the console. This is the message that will be printed when the command runs.
- Return Value
- static::CODE_SUCCESS is returned to indicate that the command executed successfully.
Step 3: Register the Command
Now that we’ve created the command class, we need to ensure its properly registered in the system. Spryker uses the ConsoleDependencyProvider to register commands.
Here’s an example of how to do that:
your-spryker-project/src/Pyz/Zed/Console/ConsoleDependencyProvider.php
1 |
use Pyz\Zed\BluethinkInc\Communication\Console\BluethinkIncConsole; |
1 |
$commands [] = new BluethinkIncConsole(); |
This code tells Spryker to load the BluethinkIncConsole command whenever the console is run.
Step 4: Execute the Command
After creating and registering the command, you can execute it from the terminal like this:
1 2 3 |
docker/sdk cli console c:e console BluethinkInc:Showmessage |
Step 5: Testing and Debugging
After executing the command, ensure that:
- The message appears in the terminal.
- The command behaves as expected, i.e., no errors occur and the correct output is shown.
If you encounter any issues, you can debug by adding more detailed logging or breakpoints.
Conclusion
In this tutorial, we demonstrated how to create a custom Symfony console command within a Spryker module. The BluethinkInc:Showmessage command was created as a simple example to print a custom message. We also discussed the necessary configuration and how to register the command within the system.
Custom console commands can be a powerful tool for automating backend processes, providing quick access to business logic, or triggering important tasks directly from the command line.
Now that you know how to create your own commands, you can extend this example to create more complex commands to meet the needs of your Spryker application.
bluethinkinc_blog
2025-01-13