Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Symfony 3.3 Features: Components and Bundle

July 25, 2017

5 Min Read
Reading Time: 5 minutes

PHP can’t flourish without its frameworks. The evolution of PHP frameworks like Symfony, Laravel and Yii is a solid confirmation of this fact. In many ways, these modern frameworks have kept PHP alive, and have remained top priority for developers to use on Best PHP Hosting.

symfony 3.3 features

Symfony offered a major release back in 2016 that introduced new directory structure. This was the start of the popularity of the framework in the PHP dev community. Now Symfony has released the latest stable release, version 3.3.x with several new components and a bundle. In addition to these, the release has implemented a number of updates and fixes. The community is very excited about Symfony 3.3 features, components and bundles.

In this article, I will discuss the installation process, components and bundles. I am really excited about a new component, dotenv that I will also cover in this article.

Install Symfony 3.3

To install symfony 3.3, you need to launch a PHP application on Cloudways ( after completing the easy  registration process). Once the application has been launched, you can SSH into it and move to the app folder (you could find the details in application access area).

At this point, you have two choices for installing Symfony 3.3. You could either use the Symfony Installer or use standard installation command. At Cloudways, the root access is disabled so you can install Symfony through the following Composer command:

composer create-project symfony/framework-standard-edition your_project_name "3.3.*"

This command will install the stable release of Symfony 3.2. Once the installation finishes, go to the Application Access details and click the application URL with suffix (/web)

You will see the homepage screen of Symfony 3.3.5 (the latest stable version at the time of writing this article).

I will now discuss the new components and features introduced in Symfony 3.3.

Related: How To Upgrade From Symfony 2.X To 3.X

Dotenv Component For Environment Variables

If you are familiar with Laravel, you should know that the framework handles environment variables through an .env file. Once the config values such as database credentials and API have been added to this file, they are available globally in the Laravel application.

In this version, Symfony has introduced the dotenv component which work pretty much like the Laravel’s .env file. Dotenv Component parses .env files to make environment variables of these files accessible via getenv(), $_ENV or $_SERVER


You can install this great component in two ways:

  1. Composer is the preferred way of installing dotenv component. Use the following command to add it to your projects:
    composer require symfony/dotenv

2. You can also clone it from GitHub by running the following command:

$ git clone

After successful installation, you need to require the vendor/autoload.php file to enable the autoloading mechanism provided by Composer. Otherwise, your application won’t be able to find the classes of this Symfony component.

Using Dotenv

It is a good practice to save and parse sensitive credentials from a single location without interdependability. Dotenv provides a `load()` method through which you can load the .env files in PHP applications.

For this, add the following code:

use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();


// You can also load several files

$dotenv->load(__DIR__.'/.env', __DIR__.'/');

You need to initialize the dotenv class by creating the instance. After that, define the directory path of the .env file(s) in the load() method. The Symfony’s .env file is same as the Laravel’s. Just define the attributes and values as:

Now you can easily access the values via `getenv()` method :

$dbUser = getenv('DB_USER');

The good thing is that the Symfony Dotenv component does not override the existing environment variables.

The Lock Component

Symfony uses Locks to prevent more than one simultaneous execution of commands on the server. This functionality was first added in Symfony 2.6 and is now extended through a whole new component with three main features:

  1. Supports multiple lock stores (flock(), PHP Semaphore extension, Redis and Memcache)
  2. Creates Blocking, nonblocking and auto-expiring locks.
  3. Allows combining several stores

Using Lock

To use the lock component independently, you need to define the store to use, and then create a factory for creating locks:

use Symfony\Component\Lock\Factory;

use Symfony\Component\Lock\Store\SemaphoreStore;

$store = new SemaphoreStore();

$factory = new Factory($store);

After that, you can acquire, renew, check and release the locks

$lock = $factory->createLock('pdf-invoice-generation');

if ($lock->acquire()) {

   // The resource "pdf-invoice-generation" is locked.

   // You can compute and generate invoice safely here.



Using the locks within Symfony is pretty straightforward. Just define the type of single or multiple locks to use in `config.yml`:


   # these are all the supported flock stores

   lock: 'flock'

   lock: 'semaphore'

   lock: 'memcached://m1.docker'

   lock: ['memcached://m1.docker', 'memcached://m2.docker']

   lock: 'redis://r1.docker'

   lock: ['redis://r1.docker', 'redis://r2.docker']

After that, use the service in Symfony’s container to perform the same process i.e acquire, renew, check and release the locks

// non-blocking, non-expiring

$lock = $container->get('lock')->acquire();

// blocking, non-expiring (waits indefinitely until the lock is acquired)

$lock = $container->get('lock')->acquire(true);

// non-blocking, expiring in 60 seconds (unless the lock is refreshed)

$lock = $container->get('lock')->acquire(false, 60);

// normal operations on the lock




Note: This component might not be available in Symfony 3.4.

The WebLink Component

The Weblink component manages links between resources including assets, advising browsers to preload and prefetch resources through HTTP and HTTP/2 pushes. The component implements these specifications: HTML5 Links, Preload, and Resource Hints.

All these features in Symfony 3.3 can be used via several Twig functions:

  • link(), adds the Link HTTP header
  • preload(), preload resources (not only assets)
  • dns_prefetch(), resolves a resource origin as early as possible
  • preconnect(), initiates an early connection to a resource (including DNS resolution, TCP handshake, and TLS negotiation)
  • prefetch(), indicates to the client that it should prefetch the given resource
  • prerender(), indicates to the client that it should prerender the given resource.

Out of these three components, I personally welcome the Dotenv component the most. The absence of a Laravel’s .env file like functionality was a serious shortcoming of Symfony that was fied through this component.

Symfony 3.3 has also introduced new bundle for running console commands in a more simplified manner.

The WebServerBundle

A long while ago, PHP 5.4 introduced the builtin web server which allowed developers to run PHP apps locally without configuring a full Apache or Nginx web server. Following the trend, Symfony also introduced console commands for similar purpose. In Symfony 3.3, all these commands are moved to the WebServerBundle.

The web server now stores its address in a PID file stored in the current directory. In addition, the server:start command now looks for a free available port automatically. In the earlier versions, the server start command needed a port number and IP address manually:

bin/console server:start

Now, in Symfony 3.3, the server looks for the free port by itself, and then initiates itself

bin/console server:start

Final Words

Symfony has gain a lot of momentum after the new updates. At the moment, it has 9M+ downloads. Recently Taylor otwell has also praised the framework and highlights the soft comparison between Laravel and Symfony. All fixes and updates for this Symfony 3.3 are available on the official blog.

If you have any question and queries, feel free to comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!