Symfony 2.8.x to 3.4.x

by Tim Davis on Feb 19, 2018

One of my current projects is using Symfony 2.8.21 LTS and I need to upgrade to 3.4.x LTS. I found some generic guides online but these did nothing other than spit out a generous amount of errors.

Prep

To prepare for this update I downloaded a base copy of Symfony 3.4.4. I do this with PHPStorm but any method of installing the skeleton for 3.4.4 should work.

All commands start with /, where / is used to represent the root of the project, not the Operating System.

Directories

Symfony 3.4.4 moves some directories around, namely the /app/cache and /app/log directories. These directories have been moved to a /var directory.

  1. Copy /var directory from 3.4.4 to /
    • The /var directory contains the following
      • /var/cache
        • /var/cache/.gitkeep
      • /var/logs
        • /var/logs/.gitkeep
      • /var/sessions
        • /var/sessions/.gitkeep
      • /var/bootstrap.php.cache
      • /var/SymfonyRequirements.php

Kernel

The Kernel has a few new methods used, these methods will need added to your kernel.

  1. Open 3.4.4/app/AppKernel.php and review the following methods. These can be straight copied to your existing /app/AppKernel.php if you do not have any special locations set already.
    • getRootDir()
    • getCacheDir()
    • getLogDir()
  2. Compare the registerContainerConfiguration(LoaderInterface $loader) in 3.4.4/app/AppKernal.php and /app/AppKernel.php, there should be about 5 lines that need added. Example below:

    $loader->load(function (ContainerBuilder $container) {
        $container->setParameter('container.autowiring.strict_mode', true);
        $container->setParameter('container.dumper.inline_class_loader', true);
    
        $container->addObjectResource($this);
    });

Configurations

You will need to compare all files in /app/config to the files in 3.4.4/app/config. Some of these files will have a few changes, be careful not to overwrite your own settings!!!

Services

Pay special attention to the services.yml file as the format for defining services has changed. See Symfony Services for help formatting your services.

Web Files

If you don't have any local changes to either /web/app.php or /web/app_dev.php you can copy these files from the 3.4.4 base install. Otherwise, you will need to compare and change accordingly.

Composer

This is the most important part. My application was simple and only used the base Symfony packages, you may need to do some research to find out what versions you need for any additional packages. Below is the default 3.4.4 required packages, find the same package in your /composer.json file and replace as needed.

...
    "require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^5.0.0",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.6.4",
        "symfony/symfony": "3.4.*",
        "twig/twig": "^1.0||^2.0"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0"
    },
...

Once the replacements are made I delete the /vendor directory and then run php composer.phar install or composer install (depending on your flavor). This will install all the new packages.

Final Steps

You may need to reset the permissions on the /var directory, see Setting up or Fixing File Permissions.

Note: A new paramater logout_on_user_change: true may need to be added to your firewall.

Note: Pay attention to your depreciated code warning in your debug toolbar

At this point you should be good to spin up your application.


Keep in mind that this blog is not meant as a full fledged teaching resource, it's mainly used as my brain dump so I don't forget things.

If you find it useful then awesome!