How to Upgrade From Symfony 2.x to 3.x

by Shahroze Nawaz  August 17, 2016

In the last installment, I used AuthO for increasing security for Symfony 3.1 based apps.

Versions 3.0.x and 3.1.x of Symfony are out for developers and the community is all abuzz with the neat features introduced for developers in these versions. I have already covered the features introduced in Symfony 3.1.

There are still developers who are working on the older 2.x version of the framework. In order to take the full advantage of the features of the 3.x versions of Symfony platform, it is important that developers upgrade Symfony to the latest version.

upgrade symfony from 2.x to 3.x

In this article, I will discuss how to upgrade from Symfony 2.7 to Symfony 2.8 and then to Symfony 3.x. An upgrade to version 2.8 is called a minor update while upgrading to version 3.x is called a major update. Version 3.x comes with a whole new directory structure that also change the location of several key files and components.

Upgrade Symfony 2.7 to 2.8

Apps created in Symfony 2.7 need to be upgraded to version 2.8 before they will upgrade to 3.x versions because version 2.7 have now some deprecated features which are not present in latest versions. Since this is a minor update, the code should remain intact with minimal issues.

To upgrade, open composer.json and replace the Symfony version  2.7 with 2.8.*

symfony upgrade version

Now, open SSH terminal and run the following command:

This command will install the latest stable version of Symfony 2.8 in the application and will update all the dependencies present in composer.json.

symfony installation message

If you don’t want to restrict the app to the beta version, just replace 2.7 with 2.8.* and run the update command. The app should work fine without any broken code. There might be some issues with forms because several form methods and properties are deprecated in version 2.8.

You might be interested in: Learn How You Can Install Symfony 3 on Cloud

Deprecated Methods for Forms in Symfony 2.8

Forms contain some properties that are deprecated in Symfony 2.8:

  • Cascade_validation attribute is deprecated. Use the constraint option with valid() method.

If you were using the above method, then it’s gone. You must use the following code:

  • Type names are also deprecated. You will now not referencing class name but their full qualified class-name.

As the above method is not recommended anymore, you should use the following:

  • Returning type instances from FormTypeInterface::getParent() is deprecated. Return the fully-qualified class name of the parent type class instead.

The above method is no more. Use the following to get desired results:

  • Passing type instances to Form::add(), FormBuilder::add() and the FormFactory::create*() methods is deprecated and will not be supported anymore in Symfony 3.0. Pass the fully-qualified class name of the type instead.

changed to.

The whole list of the deprecated methods is available at this upgrade log.

Moving to the New Directory Structure of Symfony 3.x

As mentioned in the introduction, Symfony 3.0 has a great new directory structure. You no longer have to worry about the logs and cache files and  all the code will now reside in the App folder.

The New Directory “var”

Symfony 3 has introduced a new directory called var, which now holds the logs and cache files. First you need to create the directory and then you could move the files in it

create directory in symfony

Overriding Methods in AppKernel.php

Along with creating a new directory, some methods must be overridden in app/AppKernel.php to update the application. Copy and paste the following in Appkernel.php

These methods will update the var directory and integrate it with the  Symfony core.

Move console to bin

The next step is to move the console file from app folder to bin. You must remember to update the autoloader after this.

move console to bin

This will move the console file to bin. Now open it in the editor and replace require_once __DIR__.'/bootstrap.php.cache'; with require __DIR__.'/../app/autoload.php';.

Updating Paths in composer.json File

Now, update the paths for the newly created directories in composer.json. Open it and move to extra attribute and add the following lines in it.

Finally Upgrading to Symfony 3.x

Now you are all setup to update to Symfony 3. Open composer.json and  move to Symfony version declaration and replace it. Find this line

And replace it with

Now run the update command in composer.

After the installation, run the console command and check the version of symfony.

You will see the upgraded version of symfony.

symfony bin/console command

Note: if you are facing problem to open symfony dev environment on cloudways then open web/app_dev.php file and remove the below condition. it will work as a charm now.

Final words

Upgrading symfony 2.x to 3.x version is pretty much easy. If done right, there is little chance that the move will break the code. You can also check the documentation of Knpuniversity.  I recommend that you upgrade to the latest versions because Symfony 3.1 is a highly optimized framework and has a great new directory structure. In the next part of this series, I will discuss how to use REST API in the backdrop of Symfony 3.1. Here is a short introduction and list of major topics in this Symfony 3.1 series.

If you have a query or face any issues while doing the upgrade, leave a 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...