Easy Way to Create a Symfony Console Application
How to create PHP CLI application with Symfony console component.
Today I will create a PHP console application with the Symfony Console Component.
I have looked around but could not find step by step guide to creating a simple console application, So decided to write a blog for it.
Let’s start building an application that can be run as a cron job.
Go to your Site directory and create the following app directory.
mkdir php-console-app
Navigate to the directory.
cd php-console-app
Now install The Console Component by using composer in your project.
composer require symfony/console
After installing the console component, the project structure will look like below.
The Console Component was installed but it did not create the application file itself. Like in standards Symfony application it has bin/console to interact with commands.
I will try to use the same directory structure as we get in the Symfony application.
Create bin and src directory to start adding application files.
mkdir {bin,src}
Now create a console application in the bin directory and add the following code.
vi bin/console
bin/console (this file does not need an extension)
#!/usr/bin/env php<?php// application.phprequire __DIR__.’/../vendor/autoload.php’;use Symfony\Component\Console\Application;$application = new Application();$application->run();
Now you should be able to run the following command and see the output.
php bin/console
The output of the above command.
Congratulations! You have a running console application.
Create Console Command and Register with the Console Application.
Let’s say we want to create a Hello World command.
Go to the src directory and create HelloWorldCommand.
vi src/HelloWorldCommand.php
Add the following code into your HelloWorldCommand.
<?php
namespace App;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HelloWorldCommand extends Command
{
// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:hello-world';
protected static $defaultDescription = "This command say hello world";
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln("Hello World");
return Command::SUCCESS;
}
}
Now we have command but it’s not registered yet with our console application.
Open the bin/console file and register the hello world command in the application.
$application->add(new HelloWorldCommand());
And don’t forget to add namespace at the top.
use App\HelloWorldCommand;
Now bin/console will look as below:
#!/usr/bin/env php
<?php
// application.php
require __DIR__.'/../vendor/autoload.php';
use Symfony\Component\Console\Application;
use App\HelloWorldCommand;
$application = new Application();
$application->add(new HelloWorldCommand());
$application->run();
Now try to run bin/console again.
php bin/console
You will see an error as shown below:
The reason is that the new command is not loaded yet in the composer autoload file.
We need to configure composer.json so we can load all project files as soon as we add them.
Open the composer.json file and add the following sections to it.
"autoload": {
"psr-4": {
"App\\": "src"
},
"classmap": ["src"]
}
And optimise the composer by running the following command.
composer dump -o
Now try again
php bin/console
You should be able to see app:hello-world under the app section.
Let’s try to run it.
php bin/console app:hello-world
Hurray!!!!! It works.
Congratulations! Your first PHP Console Command is working.
You can download the source code from the Github repository.
Next, you can try to deploy this app in AWS Lambda and run it as a cron job.