How to Configure Symfony Console Skeleton Project
Step by Step guide to configuring the Symfony Console Skeleton project.
If you are new to Symfony world, please follow the instructions from the documentation page.
Installing & Setting up the Symfony Framework
In this post, I will try to set up a standard skeleton project with Symfony Flex and install basic packages like The DependencyInjection Component, Doctrine and others.
List of packages to install and configure in this project.
- Symfony Flex
- The Runtime Component
- The HttpKernel Component
- Framework Bundle
- The DependencyInjection Component
- The Config Component
- The Yaml Component
Let’s start creating a project directory.
mkdir symfony-console-skeleton
Now go to the application directory.
cd symfony-console-skeleton
Installing and configuring Symfony Flex
composer require symfony/flex
Symfony Flex is a Composer plugin that set up a basic structure for the project.
It has created a very basic composer.json file.
# Basic composer file.
{
"require": {
"symfony/flex": "^2.0"
}
}
We will manage all versions of Symfony packages with Symfony Flex, so add an extra block in the composer file.
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.0.*"
}
}
This extra block will install all package versions that are compatible with Symfony 6.0
We also need to restrict our project to using a specific version of PHP. Add the following line in the required section of the composer file.
"php": "^8.1",
Now it’s time to add some project information as well in the composer file.
"name": "tanvir/symfony-console-skeleton",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Console Standard Edition\" distribution",
After adding all the above information, the composer file should look like the below.
{
"name": "tanvir/symfony-console-skeleton",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Console Standard Edition\" distribution",
"require": {
"php": "^8.1",
"symfony/flex": "^2.0"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.0.*"
}
}
}
Now we are ready to install more packages for our project.
Installing and configuring “The Console Component”
composer require symfony/console
After installing the console component, Symfony Flex creates a bin/console file with some default code as follows.
#!/usr/bin/env php
<?php
use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
return new Application($kernel);
};
As you can see it requires three different components “The Runtime Component”, “The HttpKernel Component” and Framework Bundle.
Installing and configuring “The Runtime Component”
composer require symfony/runtime
Now runtime component is loaded from autoloading.
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
The second component from this file is missing called “App\Kernel”. Let’s install this component.
Installing and configuring “The HttpKernel Component”
composer require symfony/http-kernel
I am not super happy to install HttpKernel because it’s not an HTTP application. There should be a ConsoleKernel Component but it’s not available.
Let’s stick with it and try to create the Kernel of the application.
mkdir src && touch src/ConsoleKernel.php
Add the following code in Console Kernel Class.
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class ConsoleKernel extends BaseKernel
{
use MicroKernelTrait;
}
ConsoleKernel class is extended with BaseKernel which is a part of the Framework Bundle. Let’s install this last component to move forward.
composer require symfony/framework-bundle
The framework bundle helps Symfony components to integrate fully with each to function as a full-stack framework.
At this point, we have installed all required components for the console command but there is one configuration missing.
If you try to run the following command then you will see an error that says
Attempted to load class “ConsoleKernel” from namespace “App”
php bin/console
It happens because we have introduced the App namespace in ConsoleKernel class and it is not registered yet in the application.
We need to introduce the following new section in the composer file to load all classes automatically from the App namespace.
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
You might need to regenerate the class-map again by running the following command.
composer dump-autoload --classmap-authoritative
Now try again to run the console application.
php bin/console
It is still not working because this time it is asking for one more component called “The Yaml Component”.
composer require symfony/yaml
Finally, you should be able to run the console command.
php bin/console
The basic console application is working but we need to introduce a way to automatically construct objects and use them in the application.
The next step is to introduce a very important component called “The DependencyInjection Component”.
Installing and configuring “The DependencyInjection Component”
If you don’t know about this component please read the official documentation page.
Let’s install this in our project and see how we can use it in our project.
composer require symfony/dependency-injection
The DependencyInjection offer a container service where we can register our classes to construct objects. We can register classes manually in the service container or auto-configure with “The Config Component”.
We will use “Setting up the Container with Configuration Files” settings to configure our DependencyInjection rather manually. Because we do not want our application to be dependent on this component as explained in the documentation “Avoiding your Code Becoming Dependent on the Container”.
Installing and configuring “The Config Component”
composer require symfony/config
This component helps us to load three types of configurations, PHP, YAML and XML.
We will use YAML config files for our application to configure the components which require another component called “The Yaml Component” and it’s already installed.
Now you can configure your application in Yaml files as Symfony's official skeleton project does.
Congratulations! You have learned how Symfony put together components in its skeleton project.