Symfony LTS:如何从2.8升级到3.4?

时间:2018-09-17 21:20:41

标签: symfony symfony-2.8 symfony-3.4

Symfony 2.8是2.x分支的最新版本和先前的LTS。

Symfony 3.4是3.x分支的最新版本和当前的LTS。

要将Symfony从2.8升级到3.4并切换到最后一个LTS,需要执行哪些步骤?

2 个答案:

答案 0 :(得分:6)

准备升级

检查composer.json中列出的所有依赖项和捆绑软件是否已发布与Symfony 3.4兼容的版本,可以通过在Packagist上搜索每个软件包来进行此操作,例如EasyAdmin与Symfony 3兼容,因为requires中的依赖项不限于Symfony 2(我们会看到类似symfony/*: ~2.3的东西)。如果其中一个依赖项与Symfony 3不兼容,则必须找到替换软件包或修补这些库。

composer.json

为了将您的应用程序从Symfony 2.8升级到Symfony 3.4,您必须通过更改 composer.json 文件来更新依赖项:

({[…]表示未更改的代码)

旧(2.8)版本:

{
    […]
    "autoload-dev": {
        "files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
     },
    "require": {
        "php": ">=5.3.9",
        "doctrine/doctrine-bundle": "~1.4",
        "doctrine/orm": "^2.4.8",
        "incenteev/composer-parameter-handler": "~2.0",
        "sensio/distribution-bundle": "~4.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "symfony/monolog-bundle": "^3.0.2",
        "symfony/swiftmailer-bundle": "~2.3,>=2.3.10",
        "symfony/symfony": "2.8.*",
        "twig/twig": "^1.0||^2.0"
    },
    "require-dev": {
        "sensio/generator-bundle": "~3.0",
        "symfony/phpunit-bridge": "~2.7"
    },
    "config": {
        "bin-dir": "bin",
        "platform": {
            "php": "5.6"
        },
        "sort-packages": true
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "relative",
        […]
        "branch-alias": {
            "dev-master": "2.8-dev"
        }
    }
}

新(3.4)版本:

{
    […]
    "autoload-dev": {
        "psr-4": { "Tests\\": "tests/" },
        "files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
     },
    "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"
    },
    "config": {
        "platform": {
            "php": "5.6"
        },
        "sort-packages": true
    },

    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "web",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        […]
        "branch-alias": {
            "dev-master": "3.4-dev"
        }
    }
}

摘要

  • autoload-dev.psr-4已添加(必须使用测试目录的路径进行更改)
  • Symfony和依赖项已更新
  • symfony/polyfill-apcu是新的依赖项
  • extra已更新,以便使用新的目录结构:var用于临时文件等。
  • config.bin-dir已被删除

有关升级的更多详细信息:→ 3.0→ 3.1→ 3.2→ 3.3→ 3.4

app / AppKernel.php

添加getRootDir并更新registerContainerConfiguration功能:

public function getRootDir()
{
    return __DIR__;
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
    $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}

缓存和日志

如果要将cachelogs放在var/中,则必须通过添加以下几行来更新app/AppKernel.php文件:

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

然后创建var/目录并放入一个空文件.gitkeep

并将这些更改应用于您的.gitignore文件:

/var/cache/*
/var/logs/*
!var/cache/.gitkeep
!var/logs/.gitkeep

最后一步

一旦更新了 composer.json 文件,就必须更新依赖项:

composer update

然后您可能需要刷新缓存:

php app/console cache:clear --env=dev

注意:我使用以下命令来获取 composer.json 文件:

# create Symfony "2.8.*" project in the "2.8" directory
composer create-project symfony/framework-standard-edition "2.8" "2.8.*" --no-interaction -v
# create Symfony "3.4.*" project in the "3.4" directory
composer create-project symfony/framework-standard-edition "3.4" "3.4.*" --no-interaction -v
# compare the Symfony 2.8 and 3.4 composer.json files
diff -u 2.8/composer.json 3.4/composer.json

差异也可以在GitHub上获得。

奖金:enable autowiring of services

答案 1 :(得分:2)

2019+即时升级版本

今天,您可以使用称为 Rector 的即时升级工具(我是作者)使大多数工作自动化。它为许多框架准备了一套,而Symfony框架则最为完善。还包括您可能需要的PHP升级。

您可以在How to Upgrade Symfony 2.8 to 3.4

中了解有关此特定升级路径的更多信息。