Buy

The Flex composer.json File

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.

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!

  • 2018-01-04 Peter Kosak

    I am glad that I have experienced this. I am still learning so this is a lesson for me.
    You guys doing great job anyway!!!

  • 2018-01-04 Victor Bocharsky

    Haha, you guys probably shouldn't apply changes to the production until the course is completely published :D

  • 2018-01-04 Peter Kosak

    Aye thats what I am doing, just waiting for more people with comments so maybe they will have similar issues.

    I also regret I didnt wait till whole course is published.

  • 2017-12-26 weaverryan

    Haha, yep, sorry about that. Bad time with the holidays! The next chapter is out tomorrow :D

  • 2017-12-26 kaizoku

    Same here, application broken !
    At the end I think it was a false good idea to start this course before it was completly published.
    Ha ha ha !!
    So I'm just gonna wait and pray for a quick release.
    Cheers up KNP team ;)

  • 2017-12-25 weaverryan

    Yo Peter!

    Yea, sorry about that :). You should definitely create a new branch so you can do all of the Symfony 4 upgrade craziness over on that branch. If ALL else fails, revert *all* of your changes (including composer.json and composer.lock), then completely remove the vendor/ directory and run composer install. This will completely reset your vendor. directory.

    But yea, we'll have the rest of the tutorial out soon - it's a bit of tricky surgery to get all the steps down for this one-time upgrade to the Flex structure. So, definitely come back! The new structure is totally worth it :).

    Cheers!

  • 2017-12-25 weaverryan

    Hey Peter!

    No worries - I'm sure we can figure this out! Actually, the issue is you're just a bit ahead of us :). We will see this issue a few chapters from now (and I know, we're still releasing so you can't see it yet!). The problem is that we actually *uninstalled* the old "server:run" code. Later, we will install the NEW code and re-run this command. At this point, it *should* be broken. The solution (which you'll see later), is to run composer require server. Then, close the terminal with the old web server, open a new terminal and re-run bin/console server:run. This will restart the web server (but look at what port it says it's running on - it may not be 8000, because the old web server command gets SO confused, it may still be running on port 8000 until you restart).

    Cheers!

  • 2017-12-23 Peter Kosak

    Hi Guys,

    for beginners be very careful here. I am new to symfony, composer and console. I was trying to develop my old project that wasnt using any framework to symfony. I was skipping tutorials based on what I needed. Everything was working in symfony version 3 what I learned and I was really happy how easy it is.

    Then I wanted to upgrade to symfony 4 so first thing that will fail is your build in webserver that will run 5.6.30 on sierra so you need to upgrade it to run composer I think. Thats probably where things get messed up. Because my php -v still showed 5.6.30 however I installed 7.2 somehow I managed to show 7.2 in terminal after I typed php -v so I continue to watched this video/tutorial. Today I managed to broke my whole project when running composer update. I cant even run php bin/console server:run that was working on symfony 3 version because bin/console didnt even exist so be very carefull I have no idea what I did because I was trying to fix it and probably broke other 10 thinks. Lesson learned for me to run backups and I can start from scratch.

    Also this is probably why I do not trust framework. It is so easy to to break things. When you are not using framework and you will remove file you know it will break something but here I have no idea what will happen when I delete 1 file somewhere deeeeep in vendor folder.

    Well at least it will force me to wait and watch symfony 4 tutorial here so I can start again.

  • 2017-12-22 Peter Kosak

    After this video I refreshed my project in the browser and this is what I've got.


    Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

    Fatal error: Unknown: Failed opening required '..../vendor/symfony/symfony/src/Symfony/Bundle/WebServerBundle/Resources/router.php' (include_path='.:/usr/local/php5/lib/php') in Unknown on line 0

    After googling I found out that I have exactly this issue: https://github.com/symfony/...

    But I am now stuck because I have no idea what needs to be done and I am kind of useless with composer and github generally.

    Everything whats in this video I have done and saw same messages on my mac but could someone point me what should I do.
    Also please explain it like I was 6yrs old.

    my composer.json if thats needed.


    {
    "name": "peterkosak/myproject",
    "license": "proprietary",
    "type": "project",
    "autoload": {
    "psr-4": {
    "AppBundle\\": "src/AppBundle",
    "App\\": "src/"
    },
    "classmap": [
    "app/AppKernel.php",
    "app/AppCache.php"
    ]
    },
    "autoload-dev": {
    "psr-4": {
    "Tests\\AppBundle\\": "tests/AppBundle",
    "App\\Tests\\": "tests/"
    },
    "files": [
    "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
    ]
    },
    "require": {
    "php": "^7.1.3",
    "ext-iconv": "*",
    "symfony/console": "^4.0",
    "symfony/flex": "^1.0",
    "symfony/framework-bundle": "^4.0",
    "symfony/lts": "^4@dev",
    "symfony/yaml": "^4.0",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-migrations-bundle": "^1.2",
    "doctrine/orm": "^2.5",
    "sensio/framework-extra-bundle": "^3.0.2",
    "symfony/monolog-bundle": "^3.1.0",
    "symfony/polyfill-apcu": "^1.0",
    "symfony/swiftmailer-bundle": "^3.1",
    "twig/twig": "^1.0||^2.0"
    },
    "require-dev": {
    "symfony/dotenv": "^4.0",
    "symfony/thanks": "^1.0",
    "doctrine/doctrine-fixtures-bundle": "^2.4",
    "symfony/phpunit-bridge": "^4.0"
    },
    "config": {
    "preferred-install": {
    "*": "dist"
    },
    "sort-packages": true
    },
    "replace": {
    "symfony/polyfill-iconv": "*",
    "symfony/polyfill-php71": "*",
    "symfony/polyfill-php70": "*",
    "symfony/polyfill-php56": "*"
    },
    "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": {
    "id": "01C1ZF996JGZVMYFK23DZBEG0A",
    "allow-contrib": false
    }
    }
    }