Upgrading Existing Applications to Symfony Flex ¶
Using Symfony Flex is optional, even in Symfony 4, where Flex is used by default. However, Flex is so convenient and improves your productivity so much that it's strongly recommended to upgrade your existing applications to it.
Symfony Flex recommends that applications use the following directory structure, which is the same used by default in Symfony 4, but you can customize some directories:
                    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  | 
                
                    your-project/
├── assets/
├── bin/
│   └── console
├── config/
│   ├── bundles.php
│   ├── packages/
│   ├── routes.yaml
│   └── services.yaml
├── public/
│   └── index.php
├── src/
│   ├── ...
│   └── Kernel.php
├── templates/
├── tests/
├── translations/
├── var/
└── vendor/
                     | 
            
This means that installing the symfony/flex dependency in your application
is not enough. You must also upgrade the directory structure to the one shown
above. There's no automatic tool to make this upgrade, so you must follow these
manual steps:
Install Flex as a dependency of your project:
プロジェクトの依存関係として Flex をインストールします。1
$ composer require symfony/flexIf the project's
composer.jsonfile containssymfony/symfonydependency, it still depends on the Symfony Standard Edition, which is no longer available in Symfony 4. First, remove this dependency:プロジェクトの composer.json ファイルに symfony/symfony 依存関係が含まれている場合でも、Symfony 4 では使用できなくなった Symfony Standard Edition に依存しています。まず、この依存関係を削除します。1
$ composer remove symfony/symfonyNow add the
symfony/symfonypackage to theconflictsection of the project'scomposer.jsonfile as shown in this example of the skeleton-project so that it will not be installed again:次に、symfony/symfony パッケージをプロジェクトの composer.json ファイルの競合セクションに追加します (このスケルトン プロジェクトの例に示されているように)、再インストールされないようにします。1 2 3 4 5 6 7 8
{ "require": { "symfony/flex": "^1.0", + }, + "conflict": { + "symfony/symfony": "*" } }Now you must add in
composer.jsonall the Symfony dependencies required by your project. A quick way to do that is to add all the components that were included in the previoussymfony/symfonydependency and later you can remove anything you don't really need:ここで、プロジェクトに必要なすべての Symfony 依存関係を composer.json に追加する必要があります。これを行う簡単な方法は、以前の symfony/symfony 依存関係に含まれていたすべてのコンポーネントを追加することです。その後、本当に必要のないものはすべて削除できます:1 2 3
$ composer require annotations asset orm-pack twig \ logger mailer form security translation validator $ composer require --dev dotenv maker-bundle orm-fixtures profilerIf the project's
composer.jsonfile doesn't contain thesymfony/symfonydependency, it already defines its dependencies explicitly, as required by Flex. Reinstall all dependencies to force Flex to generate the configuration files inconfig/, which is the most tedious part of the upgrade process:プロジェクトの composer.json ファイルに symfony/symfonydependency が含まれていない場合、Flex で必要とされるように、依存関係が既に明示的に定義されています。すべての依存関係を再インストールして、Flex が config/ に構成ファイルを生成するように強制します。これは、アップグレード プロセスの中で最も面倒な部分です。1 2
$ rm -rf vendor/* $ composer install- Regardless of which of the previous steps you followed, at this point you'll have
lots of new config files in 
config/. They contain the default config defined by Symfony, so you must check your original files inapp/config/and make the needed changes in the new files. Flex config doesn't use suffixes in config files, so the oldapp/config/config_dev.ymlgoes toconfig/packages/dev/*.yaml, etc.前のどの手順に従ったかに関係なく、この時点で、config/ に多くの新しい構成ファイルができます。それらには Symfony によって定義されたデフォルトの構成が含まれているため、app/config/ で元のファイルを確認し、新しいファイルに必要な変更を加える必要があります。 Flex config は設定ファイルでサフィックスを使用しないため、古い app/config/config_dev.yml は config/packages/dev/*.yaml などに移動します。 The most important config file is
app/config/services.yml, which now is located atconfig/services.yaml. Copy the contents of the default services.yaml file and then add your own service configuration. Later you can revisit this file because thanks to Symfony's autowiring feature you can remove most of the service configuration.最も重要な構成ファイルは app/config/services.yml で、現在は config/services.yaml にあります。デフォルトの services.yaml ファイルの内容をコピーしてから、独自のサービス構成を追加します。Symfony の自動配線機能のおかげで、ほとんどのサービス構成を削除できるため、後でこのファイルにアクセスできます。Note
ノートMake sure that your previous configuration files don't have
importsdeclarations pointing to resources already loaded byKernel::configureContainer()orKernel::configureRoutes()methods.以前の構成ファイルに、Kernel::configureContainer() または Kernel::configureRoutes() メソッドによって既にロードされているリソースを指す importsdeclarations がないことを確認してください。Move the rest of the
app/contents as follows (and after that, remove theapp/directory):残りの app/ コンテンツを次のように移動します (その後、app/ ディレクトリを削除します)。app/Resources/views/->templates/app/Resources/views/ -> テンプレート/app/Resources/translations/->translations/アプリ/リソース/翻訳/ -> 翻訳/app/Resources/<BundleName>/views/->templates/bundles/<BundleName>/app/Resources//views/ -> テンプレート/バンドル//- rest of 
app/Resources/files ->src/Resources/残りの app/Resources/ ファイル -> src/Resources/ 
Move the original PHP source code files from
src/AppBundle/*, except bundle specific files (likeAppBundle.phpandDependencyInjection/), tosrc/and update the namespace of each moved file to beApp\...(advanced IDEs can do this automatically).元の PHP ソース コード ファイルを src/AppBundle/* から移動し、バンドル固有のファイル (AppBundle.php や DependencyInjection/ など) を除き、tosrc/ に移動し、移動した各ファイルの名前空間を App\... に更新します (高度な IDE はこれを自動的に実行できます)。 )。In addition to moving the files, update the
autoloadandautoload-devvalues of thecomposer.jsonfile as shown in this example to useApp\andApp\Tests\as the application namespaces.ファイルの移動に加えて、この例に示すように composer.json ファイルの autoload および autoload-devvalues を更新して、アプリケーションの名前空間として App\ および App\Tests\ を使用します。If you used multiple bundles to organize your code, you must reorganize your code into
src/. For example, if you hadsrc/UserBundle/Controller/DefaultController.phpandsrc/ProductBundle/Controller/DefaultController.php, you could move them tosrc/Controller/UserController.phpandsrc/Controller/ProductController.php.複数のバンドルを使用してコードを整理した場合は、コードを src/ に再編成する必要があります。たとえば、src/UserBundle/Controller/DefaultController.php と src/ProductBundle/Controller/DefaultController.php がある場合、それらを src/Controller/UserController.php と src/Controller/ProductController.php に移動できます。- Move the public assets, such as images or compiled CSS/JS files, from
src/AppBundle/Resources/public/topublic/(e.g.public/images/).画像やコンパイル済みの CSS/JS ファイルなどのパブリック アセットを src/AppBundle/Resources/public/ から public/ (例: public/images/) に移動します。 - Remove 
src/AppBundle/.src/AppBundle/ を削除します。 - Move the source of the assets (e.g. the SCSS files) to 
assets/and use Webpack Encore to manage and compile them.アセットのソース (SCSS ファイルなど) を assets/ に移動し、Webpack Encore を使用してそれらを管理およびコンパイルします。 SYMFONY_DEBUGandSYMFONY_ENVenvironment variables were replaced byAPP_DEBUGandAPP_ENV. Copy their values to the new vars and then remove the former ones.SYMFONY_DEBUG および SYMFONY_ENV 環境変数は、APP_DEBUG および APP_ENV に置き換えられました。それらの値を新しい変数にコピーしてから、以前のものを削除します。- Create the new 
public/index.phpfront controller copying Symfony's index.php source and, if you made any customization in yourweb/app.phpandweb/app_dev.phpfiles, copy those changes into the new file. You can now remove the oldweb/dir.Symfony の index.php ソースをコピーして新しい public/index.php フロント コントローラーを作成し、web/app.php および web/app_dev.php ファイルでカスタマイズを行った場合は、それらの変更を新しいファイルにコピーします。古い web/dir を削除できるようになりました。 - Update the 
bin/consolescript copying Symfony's bin/console source and changing anything according to your original console script.Symfony の bin/console ソースをコピーして bin/console スクリプトを更新し、元のコンソール スクリプトに従って何かを変更します。 - Remove the 
bin/symfony_requirementsscript and if you need a replacement for it, use the new Symfony Requirements Checker.bin/symfony_requirements スクリプトを削除し、その代わりが必要な場合は、新しい Symfony Requirements Checker を使用してください。 - Update the 
.gitignorefile to replace the existingvar/logs/entry byvar/log/, which is the new name for the log directory..gitignore ファイルを更新して、既存の var/logs/ エントリを、ログ ディレクトリの新しい名前である var/log/ に置き換えます。 
Customizing Flex Paths ¶
The Flex recipes make a few assumptions about your project's directory structure.
Some of these assumptions can be customized by adding a key under the extra
section of your composer.json file. For example, to tell Flex to copy any
PHP classes into src/App instead of src:
                    1 2 3 4 5 6 7  | 
                
                    {
    "...": "...",
    "extra": {
        "src-dir": "src/App"
    }
}
                     | 
            
The configurable paths are:
bin-dir: defaults tobin/bin-dir: デフォルトは bin/config-dir: defaults toconfig/config-dir: デフォルトは config/src-dirdefaults tosrc/src-dir のデフォルトは src/var-dirdefaults tovar/var-dir のデフォルトは var/public-dirdefaults topublic/public-dir のデフォルトは public/
If you customize these paths, some files copied from a recipe still may contain references to the original path. In other words: you may need to update some things manually after a recipe is installed.
Learn more ¶
- How To Configure and Use Flex Private Recipe RepositoriesFlex プライベート レシピ リポジトリを構成および使用する方法