Blog by Aniruddha

Upgrade your Symfony project from 2.x to 3.4

by Aniruddha on 2018-07-11T00:12:57.000Z

Upgrade your Symfony project from 2.x to 3.4

This article may seems to you extra, as the topic has been covered in many places. There are many other and preferred ways to do this. The reason why I chose the topic, is to show what are the difficulties I have faced while migrating one project. So...hang tight: From Symfony 3.x, the symfony installaer came into light. So to install a Symfony 3.4 version you can opt for any 02 paths: 1. Using installer: Grab Symfony installer

https://github.com/symfony/symfony-installer c:\bin> php -r "fileputcontents('symfony', filegetcontents('https://symfony.com/installer'));" c:\bin> (echo @ECHO OFF & echo php "%~dp0symfony" %*) > symfony.bat

Install Symfony 3.4 cd to htdocs(I used xampp) and run

symfony new yourprojectname 3.4

Symfony3.4-via-installer-2 Symfony3.4-via-installer 2. Using composer:

composer create-project symfony/framework-standard-edition yourprojectname/ "3.4.*"

Symfony3.4-via-composer.png Symfony3.4-via-composer-2 Now once to change directory to yourprojectname, first thing you have to do is to create bundle. You might have installed custom bundle in your Symfony 2.x phase:

php bin/console generate:bundle --namespace=YourNameSpace/YourBundle

This will ask several inputs, only value to provide is configuration format which should be yml (you may need to try twice) Symfony3.4-generate-bumdle Now you will face and autoload issue: It will not able to change autoload and the message below will be displayed: The command was not able to configure everything automatically. Symfony3.4-aotuload-issue-generate-bumdle Now open composer.json and locate the autoload section: Symfony3.4-aotuload-issue-composer Change it to "psr-4": { "": "src/" }, Which will looks like: Symfony3.4-aotuload-issue-resolved-composer Now run composer update, see what happened in following figure: Symfony3.4-resolve-aotuload-issue-composer-update Acquire dependent bundles Now we must acquire all dependent bundles, need to run

composer require sonata-project/doctrine-orm-admin-bundle

and so on..... While configure, you will notice many deprecation issue. It depends on bundle configurations. For an example: To remove deprecation, found this https://stackoverflow.com/questions/47698006/symfony-3-4-refreshing-a-deauthenticated-user-is-deprecated, security.yml should looks like:

main: logoutonuser_change: true anonymous: ~

Import & configure DB Now it’s time to import database: Run

php bin/console doctrine:database:create --connection=default

This will create database, now import the existing database into newly created one Now copy doctrine folder and paste into src/yourbundle/Resources/config folder run

php bin/console doctrine:generate:entities YourNameSpace/YourBundle php bin/console doctrine:schema:update –force

Database is configured, now setup vhost and try in browser (add appdev.php in .htaccess and comment line #13-19 in appdev.php) Now copy all folders from bundle, except DependencyInjection, Entity, Resources etc. Move views and public assets Copy src/Bundle/Resources/public directory to same location in Symfony 3.4 and run below command

php bin/console assets:install web php bin/console cache:clear --env=dev

Copy existing src/Bundle/Resources/views files to app/Resources/views Remove all YourNameSpaceYourBundle::” bundle scope from twigs as all templates moved to app/Resources from src/Bundle namespace That's all as of now. Stay tuned !!!