Symfony 3.3 Features: Components and Bundle

by Shahroze Nawaz  July 25, 2017

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 a top priority for developers

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:

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

Installation

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:

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

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:

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 :

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:

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

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

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

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:

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

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.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...