Skip to content

Instantly share code, notes, and snippets.

@sentenza
Last active January 23, 2017 15:13
Show Gist options
  • Save sentenza/77b38a1a6e77108d1f7e3218f3f6d680 to your computer and use it in GitHub Desktop.
Save sentenza/77b38a1a6e77108d1f7e3218f3f6d680 to your computer and use it in GitHub Desktop.
How to upgrade from Symfony2 to Symfony3

Before updating to Symfony 3

General fixes

You need to launch this Symfony2 version fixer.

Security context fix

Then you have to change every occurrency of security.context with security.token_storage (see also this SO answer http://stackoverflow.com/a/29672308/1977778). You must update your codebase to match the security component improvements of version 2.6, as described here.

  // Symfony 2.5
  $user = $this->get('security.context')->getToken()->getUser();
  // Symfony 2.6
  $user = $this->get('security.token_storage')->getToken()->getUser();

  // Symfony 2.5
  if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { ... }
  // Symfony 2.6
  if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { ... }

Composer.json

To update to v3 you need to put Symfony 3+ as a dependency on composer.json. At the time this document was written is "symfony/symfony": "3.2.*" and, if you're using JMS/TranslationBundle you also need to avoid updating Twig to v2. So, you need to put only a 1+ compatibility with "twig/twig": "~1.31".

AppKernel.php

Add the following methods to AppKernel.php, if they are not here yet.

  public function getRootDir()
  {
      return __DIR__;
  }

  public function getCacheDir()
  {
      return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
  }

  public function getLogDir()
  {
      return dirname(__DIR__).'/var/logs';
  }

Form_enctype is no more supported

You can replace

<form method="post" {{ form_enctype(decryptionForm) }}>

by

{{ form_start(decryptionForm) }}

and

</form>

by

{{ form_end(decryptionForm) }}

Form entities

Change the choice_label attribute on form type with the following callable action.

'choice_label' => function ($value, $key, $index)

Sessions and CSRF token

https://codereviewvideos.com/blog/fix-csrf-token-is-invalid/

References

  1. https://labs.kunstmaan.be/blog/how-to-upgrade-your-project-to-symfony-3
  2. http://stackoverflow.com/a/36402657/1977778
  3. http://stackoverflow.com/a/37816982/1977778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment