Upgrading Symfony to Latest Long Term Support (LTS) version

Upgrading Symfony to Latest Long Term Support (LTS) version

Tudip

Tudip

24 July 2020

Upgrading from Symfony 2.6 to the latest version is going to be a little bit of a tedious task. It has been long since Symfony 2.6 was introduced, so there are going to be a lot of changes in the latest version.

We will be covering the major changes in this blog. Instead of upgrading directly to the Symfony LTS version, first we will upgrade to Symfony 2.8, then 3.4, then Symony 4.4.

Of Course this is going to take a while, but this is the best way to move to the latest version. Although Symfony tries its best to avoid breaking backward compatibility, there are certain things that need to be changed. So, without further due, let’s begin.

Upgrading to Symfony 2.8

Head to  composer.json and modify,

“require”: {

“symfony/symfony”: “2.8.*”

}

Open your terminal and run the command,

composer update symfony/symfony --with-dependencies

This will update your dependencies as well.

You can check the Symfony version with app/console –version.

Now let’s see if our project is still up and running. Well, it should as there aren’t many changes except one major change, i.e, the choiceType field.

  • The “choices” option convention was changed from value => label to label => value.

For other minor changes, you can refer to https://github.com/knpuniversity/blog/blob/master/upgrading-symfony-2.7.md, it covers almost all the changes.

If everything is alright, it’s time to move to Symfony 3, but before that we should get rid of deprecations in the code.

Upgrading to Symfony 3

Deprecations:

  • Type names are deprecated and now you should use Fully Qualified Class Name in Symfony forms.
    Before:

    $form = $this->createFormBuilder()
    
        ->add('name', 'text')
    
        ->add('age', 'integer')
    
        ->getForm();

    Now, you should change it to:

    use Symfony\Component\Form\Extension\Core\Type\IntegerType;
    
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    
    $form = $this->createFormBuilder()
    
        ->add('name', TextType::class)
    
        ->add('age', IntegerType::class)
    
        ->getForm();

    Further information on Type names can be found in the upgrade guide: https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md

  • Another change is that FormTypeInterface::getName() is replaced by FormTypeInterface::getBlockPrefix()
  • Passing type instances to Form::add(), FormBuilder::add() and the FormFactory::create*() methods are deprecated and will not be supported anymore in Symfony 3.0. Pass the fully-qualified class name of the type instead.
    Before:

    $form = $this->createForm(new MyType());

    After:

    $form = $this->createForm(MyType::class);

    Make other changes in the code according to the upgrade guide before moving to Symfony 3 as all the deprecations are removed in this version. So, it is really important.

yaml:

  • Using a colon in an unquoted mapping value leads to a ParseException.
  • Starting an unquoted string with @, `, |, or > leads to a ParseException.

Refer to this upgrade guide for further clarification : https://github.com/symfony/symfony/blob/3.0/UPGRADE-3.0.md

Moving to Symfony 3 directory structure

The Var directory:

  • The var directory now holds the logs and cache files instead of app directory.
  • Create the directory in the root folder and move the files in it.
    • mkdir var
    • Mv app/cache var/
    • Mv app/logs var/
  • To update the project and the changes to take place, make the following changes in app/Appkernel.php
    public function getRootDir()    {        return __DIR__;    }
    
    public function getCacheDir()
    
        {
    
            return dirname(__DIR__).'/var/cache/'.$this->environment;
    
        }
    
    public function getLogDir()
    
        {
    
            return dirname(__DIR__).'/var/logs';
    
        }
  • Now, we need to update the paths in composer.json as well
    In composer.json, under the extra attribute add the following lines

    • “symfony-var-dir”: “var”,
    •  “symfony-bin-dir”: “bin”
  • Move console to bin
    • Mv app/console bin/console
    • Open it and replace require_once __DIR__.’/bootstrap.php.cache’; with require __DIR__.’/../app/autoload.php’;

Now for the next big step:

Make changes in composer.json file,

“require”: {

“symfony/symfony”: “3.4.*”

}

We are moving to version 3.4 because it’s the long term support version and it’s recommended.

While running the command “composer update” ,there might be some errors while upgrading to this version, because of the dependencies.

It might be a good idea to navigate to https://packagist.org/ and look for the standard versions of the bundles and make changes in composer.json accordingly.

For example: You might get the error “sensio/distribution-bundle requires Symfony/process at version 2.2”, So all you need to do is install Symfony/process bundle first and after that run  “composer require sensio/distribution-bundle ” again, this time it should work just fine.

You will need to deal with every error like this for other bundles as well, it might be frustrating at first but after getting done with it you will feel a sense of relief.

  • Finally run, composer update symfony/symfony –with-dependencies.
  • Also, consider upgrading the PHP version as most of the bundles don’t support versions below PHP 7.0.

Upgrading to Symfony 4

The only difference between Symfony 3.4 and Symfony 4 other than the directory structure is that all the deprecations are removed in Symfony 4.

Here is a glimpse of the differences between the directory structure of Symfony 3 and Symfony 4.

Upgrade_Symfony_to_LTS_01

Further information can be found in the upgrade guide here : https://github.com/symfony/symfony/blob/4.0/UPGRADE-4.0.md

You can either choose to move to the new directory structure or you can stay with the current one.

Although one advantage of upgrading to Symfony 4 directory structure is that you will be able to use features of Symfony flex. We won’t be going into the details of Symfony flex in this section as our main concern is to upgrade our Symfony project to the latest Long Term Support version, i.e, Symfony 4.4.

Deprecations:

  • Kernel::loadClassCache() is deprecated since version 3.3 : Head to web/app.php, you will see the deprecation message there. Remove the line $kernel->loadClassCache()
  • The ClassLoader component has been removed. Use Composer instead.

Make changes in your Composer.json file to upgrade to Symfony 4 LTS version

“require”: {

“symfony/symfony”: “4.4.*”

}

Of course you will need to make changes in the code and might have to remove or replace third party bundles in order to make your application work.

You should be fine for a while as this version has security support till November, 2023, but since Symfony 5 is released, let’s talk about some of its new features.

Symfony 5.0 features

  • String, for object-oriented strings management with an abstract unit system
  • Notifier, for sending notifications via one or more channels (email, SMS, …).

As stated earlier, upgrading Symfony to the latest LTS version is going to be a little bit time taking but on the other hand you will learn many new things that will come handy.

search

Blog Categories

Request a quote