📜 ⬆️ ⬇️

Migrating from symfony 2.0 to 2.6

In this article, I would like to talk about some of the nuances that I had to go through to migrate a project from outdated Symfony 2.0 to the current Symfony 2.6.

Dependency manager

Each symfony project has its own dependencies (bundles). In version 2.0, dependencies were specified in the deps file and were pulled up by the command:

php bin/vendors install 

Now for these purposes it is important to use Composer.
')
Download Composer to the repository root:

 php -r "readfile('https://getcomposer.org/installer');" | php 


Then we create the composer.json file in the project root with the following content:
github.com/symfony/symfony/blob/2.6/composer.json
And execute the command:

 php composer.phar update 

After that, Symfony 2.6 with all the dependencies needed for it will be pulled to our project and a composer.lock file will be created, which will contain the current versions of the downloaded dependencies.

All we have to do is add the dependencies we need to the composer.json file. This can be done manually by editing the file:

 "require": { "{ }":"{ }" }, 

Or using the command:

 php composer.phar require { }:{ } 

Now we have all the necessary bundles and we can delete the deps and deps.lock files, but for the framework to work correctly, we should also update the web / app.php and web / app_dev.php files:

app.php

 <?php use Symfony\Component\ClassLoader\ApcClassLoader; use Symfony\Component\HttpFoundation\Request; $loader = require_once __DIR__.'/../app/bootstrap.php.cache'; // Enable APC for autoloading to improve performance. // You should change the ApcClassLoader first argument to a unique prefix // in order to prevent cache key conflicts with other applications // also using APC. /* $apcLoader = new ApcClassLoader(sha1(__FILE__), $loader); $loader->unregister(); $apcLoader->register(true); */ require_once __DIR__.'/../app/AppKernel.php'; //require_once __DIR__.'/../app/AppCache.php'; $kernel = new AppKernel('prod', false); $kernel->loadClassCache(); //$kernel = new AppCache($kernel); // When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter //Request::enableHttpMethodParameterOverride(); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response); 

app_dev.php

 <?php use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Debug\Debug; // If you don't want to setup permissions the proper way, just uncomment the following PHP line // read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information //umask(0000); // This check prevents access to debug front controllers that are deployed by accident to production servers. // Feel free to remove this, extend it, or make something more sophisticated. if (isset($_SERVER['HTTP_CLIENT_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR']) || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server') ) { header('HTTP/1.0 403 Forbidden'); exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.'); } $loader = require_once __DIR__.'/../app/bootstrap.php.cache'; Debug::enable(); require_once __DIR__.'/../app/AppKernel.php'; $kernel = new AppKernel('dev', true); $kernel->loadClassCache(); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response); 

Options

For the symfony 2.0 version, the parameters were loaded from the file app / config / parameters.ini. All we need is to rename it to app / config / parameters.yml and bring it to the yml file:

Before:

 [parameters] locale = en 

After:

 parameters: locale: en 

Then in app / config / config.yml we will write the path for our file:

 imports: - { resource: parameters.yml } 

Parameters changed and connected.

Backward compatibility

There are quite a few changes from version 2.0 to 2.6, I would like to tell only about some of them.

Forms:

Some functions now raise exceptions if they are used on a form already submitted:
 add(), remove(), setParent(), bind() and setData() 


You can call them from the listener in formBuilder just before submitting the form, or slightly change the logic of your form. But as a temporary measure, you can use the following code:

 $formData = $form->getData(); $form = $this->createForm(new YourForm()); $form->setData($formData); 

After we have re-created the form and transferred all the parameters from the old form to it, we can use all the functions on it that could not be used on the already submitted form.

Validation of Forms:

Instead of using the FormBulder's defunct CallbackValidation class, we simply use an EventListener after the form has already been submitted. POST_SUBMIT.

Before:

  $builder->addValidator(new CallbackValidator(function (FormInterface $form) { $value = $form['date']->getData(); if ($value != null) { $form['date']->addError(new FormError('  ')); } })); 

After:

  $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { $form = $event->getForm(); $value = $form['date']->getData(); if ($value != null) { $form['date']->addError(new FormError('  ')); } }); 


Adding your option in the form field

When creating your option in the form field MyBundle \ Forms \ ExtensionExtensionForm.php; It is necessary to specify for which type of fields these options will be added:

 public function getExtendedType() { return 'text'; } 

If we want to add them for all field types, you must specify:

  public function getExtendedType() { return 'form'; } 

Full list of changes


github.com/symfony/symfony/blob/2.7/UPGRADE-2.1.md
github.com/symfony/symfony/blob/2.7/UPGRADE-2.2.md
github.com/symfony/symfony/blob/2.7/UPGRADE-2.3.md
github.com/symfony/symfony/blob/2.7/UPGRADE-2.4.md
github.com/symfony/symfony/blob/2.7/UPGRADE-2.5.md
github.com/symfony/symfony/blob/2.7/UPGRADE-2.6.md

Official symfony documentation: symfony.com/doc/current/cookbook/index.html

Source: https://habr.com/ru/post/258403/


All Articles