The Flex composer.json File

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $8.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

We need to make our composer.json file look like the one from symfony/skeleton. Actually, go to "Releases", find the latest release, and then click to browse the files. Now we can see the stable composer.json contents.

So... yea, this one file is all you need to start a new project. That's crazy! Flex builds the project structure around it.

Tip

Before copying the composer.json, make sure to change the branch on GitHub to the latest released version you want to upgrade to (e.g. 4.1)

Bye Bye symfony/symfony

Anyways, the most important change is that, with Flex, you stop requiring symfony/symfony. Yep, you require only the specific packages that you need. Copy all of the require lines, find our composer.json file, and paste over the php and symfony/symfony lines. Oh, and remove symfony/flex from the bottom: it's up here now.

74 lines composer.json
{
... lines 2 - 17
"require": {
"php": "^7.1.3",
"symfony/console": "^4.0",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^4.0",
"symfony/lts": "^4@dev",
"symfony/yaml": "^4.0",
... lines 25 - 34
},
... lines 36 - 72
}

The symfony/framework-bundle package is the most important: this is the core of Symfony: it's really the only required package for a Symfony app.

Go back and also copy the dotenv package from require-dev and put it in our composer.json file. This package is responsible for reading the new .env file.

74 lines composer.json
{
... lines 2 - 35
"require-dev": {
"symfony/dotenv": "^4.0",
... lines 38 - 39
},
... lines 41 - 72
}

Synchronizing the rest of Composer.json

Go back and also copy the config line and paste that here too.

74 lines composer.json
{
... lines 2 - 40
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
... lines 47 - 72
}

Skip the autoload sections for now, but copy the rest of the file. Replace the existing scripts and extras sections with this new, shiny stuff.

74 lines composer.json
{
... lines 2 - 46
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": true
}
},
... lines 67 - 72
}

Brilliant!

Autoloading src/ & src/AppBundle

Let's talk about autoload. In Symfony 3, everything lived in src/AppBundle and had an AppBundle namespace. But in Symfony 4, as you can see, everything should live directly in src/. And even though we don't have any examples yet, the namespace will start with App\, even though there's no App/ directory.

Eventually, we are going to move all of our files into src/ and refactor all of the namespaces. With PhpStorm, that won't be as scary as you think. But, it is a big change, and you may not be able to do that all at once in a real project.

So, I'm going to show you a more "gentle", gradual way to upgrade to Flex. Yep, for now, we're going to leave our files in AppBundle and make them work. But new files will live directly in src/.

Right now, the autoload key in composer.json says to look for all namespaces in src/. Make this more specific: the AppBundle\\ namespace prefix should live in src/AppBundle. Do the same in autoload-dev: Tests\\AppBundle\\ will live in tests/AppBundle.

74 lines composer.json
{
... lines 2 - 4
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
... line 8
},
... line 10
},
"autoload-dev": {
"psr-4": {
"Tests\\AppBundle\\": "tests/AppBundle",
... line 15
}
},
... lines 18 - 72
}

Why are we doing this? Because now we can go copy the autoload entry from the official composer.json file and add it below our AppBundle\\ line. Copy the new autoload-dev line also.

74 lines composer.json
{
... lines 2 - 4
"autoload": {
"psr-4": {
... line 7
"App\\": "src/"
},
... line 10
},
"autoload-dev": {
"psr-4": {
... line 14
"App\\Tests\\": "tests/"
}
},
... lines 18 - 72
}

Thanks to this, Composer can autoload our old classes and any new classes!

Scaffold the Full Structure

Ok, that was a huge step. Run composer update:

composer update

The biggest change is that we're not relying on symfony/symfony anymore. And, yep! You can see it remove symfony/symfony and start adding individual libraries.

Ah, it explodes! Don't worry about that yet: Composer did finish and Flex configured 3 new recipes!

At this point, the new Flex project is fully built... and it already works! I'll prove it to you next.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "doctrine/doctrine-bundle": "^1.6", // 1.8.1
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.1
        "doctrine/orm": "^2.5", // v2.7.2
        "fzaninotto/faker": "^1.7", // v1.7.1
        "knplabs/knp-markdown-bundle": "^1.4", // 1.6.0
        "sensio/framework-extra-bundle": "^5.0", // v5.1.3
        "stof/doctrine-extensions-bundle": "dev-master", // dev-master
        "symfony/asset": "^4.0", // v4.0.1
        "symfony/console": "^4.0", // v4.0.1
        "symfony/flex": "^1.0", // v1.0.49
        "symfony/form": "^4.0", // v4.0.1
        "symfony/framework-bundle": "^4.0", // v4.0.1
        "symfony/lts": "^4@dev", // dev-master
        "symfony/maker-bundle": "^1.0", // v1.0.2
        "symfony/monolog-bundle": "^3.1", // v3.1.2
        "symfony/polyfill-apcu": "^1.0", // v1.6.0
        "symfony/profiler-pack": "^1.0", // v1.0.3
        "symfony/security-bundle": "^4.0", // v4.0.1
        "symfony/security-csrf": "^4.0",
        "symfony/swiftmailer-bundle": "^3.1", // v3.1.6
        "symfony/translation": "^4.0", // v4.0.1
        "symfony/twig-bundle": "^4.0", // v4.0.1
        "symfony/validator": "^4.0", // v4.0.1
        "symfony/web-server-bundle": "^4.0", // v4.0.1
        "symfony/yaml": "^4.0" // v4.0.1
    },
    "require-dev": {
        "symfony/dotenv": "^4.0", // v4.0.1
        "symfony/phpunit-bridge": "^4.0", // v4.0.1
        "doctrine/doctrine-fixtures-bundle": "^3.0" // 3.0.2
    }
}