If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeWe 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)
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.
{ | |
... 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.
{ | |
... lines 2 - 35 | |
"require-dev": { | |
"symfony/dotenv": "^4.0", | |
... lines 38 - 39 | |
}, | |
... lines 41 - 72 | |
} |
Go back and also copy the config
line and paste that here too.
{ | |
... 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.
{ | |
... 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!
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
.
{ | |
... 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.
{ | |
... 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!
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.
Hey Helmis D.
It tells composer that you trust on Symfony recipes that comes from the community (https://github.com/symfony/... ). If you set it to false, it won't execute such recipes, or if you remove it, it will always ask you if you want to execute them.
Cheers!
Question: We are currently at Symfony 4.3. Will the tutorial work if I use the composer.json from that branch? Or should I use this <a href="https://github.com/symfony/skeleton/blob/4.0/composer.json">composer.json</a> from the 4.0 branch?
Also, this file seems to be structured a bit differently now. At some point Ryan says to copy the "require" section of composer.json to get the framework-bundle. But in the latest composer.json, the framework-bundle is in a separate section called "flex-require." I tried copying both the "require" and "flex-require" sections, but I then get an error with composer update
.
Hey John christensen
I think you should copy the composer.json from 4.3 release. Indeed it's different and probably it will require some adjustments but the main concepts of this tutorial still apply. Can you show me the error you got? So I can help you debugging it
Cheers!
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/symfony/issues/25515
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
}
}
}
`
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!
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.
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!
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 ;)
Haha, yep, sorry about that. Bad time with the holidays! The next chapter is out tomorrow :D
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.
Haha, you guys probably shouldn't apply changes to the production until the course is completely published :D
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!!!
// composer.json
{
"require": {
"php": "^7.1.3",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"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.9.10
"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
}
}
Question: What are these lines doing ?
`
"symfony": {
`