Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Your Flex Project is Alive!

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

Thanks to the Flex recipe for symfony/framework-bundle, we now have a fully-functional Symfony Flex app living right inside our directory! public. is the new document root, config/ has all of the configuration, and our PHP code lives in src/, including the new Kernel class.

Yep, we have our old app with all our stuff, and a new, Flex, app, which is basically empty and waiting for us to move our code into it.

Re-Order .env

Open up .env.dist. Woh! This has more stuff now! That's thanks to the recipes from DoctrineBundle, SwiftmailerBundle and FrameworkBundle. Copy the FrameworkBundle section and move that to the top. Do the same thing to .env.

25 lines .env.dist
# This file is a "template" of which env vars need to be defined for your application
# Copy this file to .env file for development, create environment variables when deploying to production
# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=12c008ecf65c043dc2b14b5eb9a115ef
#TRUSTED_PROXIES=127.0.0.1,127.0.0.2
#TRUSTED_HOSTS=localhost,example.com
###
... lines 11 - 25

We don't need to do this, but APP_ENV is so important, I want to see it first. If you start a new Flex app, it's on top.

Re-Ordering the Libs

Next, this will sound weird, but run:

composer require symfony/flex

We already have this library. I know. So... why are we doing this? It's a little trick: one of the new keys in our composer.json is sort-packages, which is set to true. Thanks to this, whenever you run composer require, it orders the packages alphabetically. By requiring a package we already have, Composer just re-ordered my packages.

Thanks Jordi!

Fixing the console

But... we still have this giant error: attempted to load SecurityBundle from AppKernel. Bummer! This happens because bin/console is still trying to boot the old app.

When you start a new Flex project, the symfony/console recipe creates the bin/console file. But, since our project already had this file, the recipe couldn't do its job.

No worries! Let's go find the new file! Go to github.com/symfony/recipes. Welcome to the official recipes repository!

Navigate to symfony, console, then bin. There it is! Copy its contents. Then, completely replace our version.

40 lines bin/console
#!/usr/bin/env php
use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
set_time_limit(0);
require __DIR__.'/../vendor/autoload.php';
if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}
... lines 17 - 40

This will boot the new application! So... does it work? Run:

./bin/console

No! But that's a new error: we are closer! This says that the autoloader expects App\AppBundle\AppBundle to be defined in AppBundle.php, but it wasn't found. That's strange... that is not the correct namespace for that class! If you look closer, it says the error is coming from a new config/services.yaml file.

Our old code - the stuff in src/AppBundle - should not be used at all by the new app yet. Open that new config/services.yaml file. It has the same auto-registration code that we're familiar with. And, ah ha! Here's the problem: it is auto-registering everything in src/ as a service, but it's telling Symfony that the namespace of each class will start with App\. But, our stuff starts with AppBundle!

For now, completely ignore AppBundle: let's get the new project working and then migrate our code.

... lines 1 - 4
services:
... lines 6 - 15
App\:
... line 17
exclude: '../src/{Entity,Migrations,Tests,AppBundle}'
... lines 19 - 28

Ok, try bin/console again:

bin/console

It's alive! We just hacked a fully-functional Flex app into our project! Now let's move our code!

Leave a comment!

44
Login or Register to join the conversation
Default user avatar

Hi, when I call bin/console I have an error:

PHP Warning: require(/var/www/html/myproject/vendor/composer/../symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php): failed to open stream: No such file or directory in /var/www/html/myproject/vendor/composer/autoload_real.php on line 66
PHP Stack trace:
PHP 1. {main}() /var/www/html/myprojectl/bin/console:0
PHP 2. require() /var/www/html/myproject/bin/console:12
PHP 3. ComposerAutoloaderInite4486242646bd266ddc47af34c7850f9::getLoader() /var/www/html/myproject/vendor/autoload.php:7
PHP 4. composerRequiree4486242646bd266ddc47af34c7850f9() /var/www/html/myproject/vendor/composer/autoload_real.php:56
PHP Fatal error: require(): Failed opening required '/var/www/html/myproject/vendor/composer/../symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php' (include_path='.:/usr/share/php') in /var/www/html/myproject/vendor/composer/autoload_real.php on line 66

I cannot find any hint in google and it blocks me to go to the next chapter :(

2 Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | nospor | posted 4 years ago

I had the same error, there was some reference to that VarDumper class that needed to be removed... But I don't remember where exactly anymore

Reply
Default user avatar

Was it connected somehow to twig?

Reply

Hmm, an error like this just should *not* happen. I mean, Composer has basically set itself up to know that a specific file exists, but then it fails because that file is not there :). I believe this is just a side effect of the upgrade process, and to fix it, you should:


rm -rf vendor/*
composer install

If you look at the error closely, it looks for the dump.php function inside vendor/symfony/symfony/... But during the Flex upgrade process, we *stop* including the symfony/symfony package, and *start* including the specific packages we need. This means that the real path to this by the end of the tutorial would be something like vendor/symfony/var-dumper/... - and that's why I think it's just some weird Composer error as we massively change the vendor/ directory.

Let me know if that helps!

Reply
Default user avatar

I was removing vendor, I was removing cache and then do composer install many times. Error didnt disappear.
For now I just did git reset --hard started from the begining. Maybe this time...

Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | nospor | posted 4 years ago

Found, it's in "autoload-dev" in composer.json: https://i.imgur.com/V1m9z6g...

1 Reply

Oh, nice find Trafficmanagertech! Yes, remove that from autoload-dev!

1 Reply
Default user avatar

No comments.... I was checking that file so many times and missed that somehow :/ Thanks the_nuts :)

Reply
MattWelander Avatar
MattWelander Avatar MattWelander | posted 4 months ago

I have followed along each step but where you get a console working, I only get

In FileLoader.php line 180:

There is no extension able to load the configuration for "when@dev" (in "/Users/mattias/Documents/www/webtools/config/package
s/monolog.yaml"). Looked for namespace "when@dev", found ""doctrine_cache", "doctrine", "fos_user", "sensio_framework_extra",
"monolog", "swiftmailer", "framework", "twig", "security"" in /Users/mattias/Documents/www/webtools/config/packages/monolog.
yaml (which is loaded in resource "/Users/mattias/Documents/www/webtools/config/packages/monolog.yaml").

and

In YamlFileLoader.php line 722:

There is no extension able to load the configuration for "when@dev" (in "/Users/mattias/Documents/www/webtools/config/package
s/monolog.yaml"). Looked for namespace "when@dev", found ""doctrine_cache", "doctrine", "fos_user", "sensio_framework_extra",
"monolog", "swiftmailer", "framework", "twig", "security"".

I also get these when running composer update (though the composer update seems to finish just fine otherwise).

Reply

Hey Mattias,

I'm afraid your Symfony version is not compatible with the new way of handling environment-specific confirguration. You'll need to upgrade Symfony first or you could just keep using the old way

Cheers!

Reply
MattWelander Avatar

I'm confused... The whole point of this screencast is to guide me through upgrading to 4.0
I have followed the steps as outlined, and all vendors successfully installed including symfony 4.

What do you mean by my version is too old?

Reply

Yes, this tutorial guides you through upgrading from Symfony 3 to 4 but you're trying to use a feature of Symfony 5.3+ which is the when keyword in config files. I don't see we use that feature in this tutorial. Could you tell me where you did you get the config code of your config/packages/monolog.yaml file?

Reply
MattWelander Avatar

I'm only trying to get to version 4.0 😂 perhaps I accidentally got a much too new version? I did not put that config file there manually, it must have been generated by composer.

Here's my composer.json:


"require": {
"php": "^7.1.3",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"doctrine/orm": "^2.5",
"friendsofsymfony/user-bundle": "~2.1",
"sensio/framework-extra-bundle": "^5.0.0",
"symfony/console": "^4.0",
"symfony/flex": "^1.19",
"symfony/framework-bundle": "^4.0",
"symfony/lts": "^4@dev",
"symfony/monolog-bundle": "^3.1.0",
"symfony/polyfill-apcu": "^1.0",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/yaml": "^4.0",
"twig/twig": "^1.0||^2.0"
},
"require-dev": {
"symfony/dotenv": "^4.0",
"symfony/phpunit-bridge": "^4.0"
},

It should all be according to the tutorial. Do I need to restrict any of these to a lower version?

Reply
MattWelander Avatar

It seems like removing the monologe.yaml all together solved the problem for now. But still curious how I ended up with it, it was autogenerated for sure. Could it be that composer first installed 5.4 (and ran recipes for that?) and only later in the process downgraded it to 4.4? I didn't follow the composer log maticulously upon install there.

Reply

Hey, sorry for my late reply, the weekend got in the middle :)
I think you're right, Symfony Flex installed a higher recipe version of monolog for some reason. I'm not sure why but perhaps monolog bundle v3.7 supports Symfony 4.4. Double-check what version of monolog you got install

Cheers!

1 Reply

Thank you. I've downgraded in composer.json to "symfony/monolog-bundle": "3.4.*", and then run composer recipes:update
solved the issue.

Reply
Gung mahendra M. Avatar
Gung mahendra M. Avatar Gung mahendra M. | posted 1 year ago

Hi,
I got error

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '256' frames in /home/user/projects/myproject/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/SymfonyFileLocator.php:84

any idea?

Reply

Hey Hendra,

Looks like you have an infinite loop somewhere in the code. Please, double check it. I think you can look at error stacktrace on the Symfony error page to get more info about the calls - this should help to figure out the problem spot. It's good that you have Xdebug installed, otherwise the page will just hang for you in infinite loop, but in your case you have just an error. So, try to debug it further to find the root of the problem

I hope this helps!

Cheers!

Reply
Michael L. Avatar
Michael L. Avatar Michael L. | posted 1 year ago

How do we handle @template in the old appbundle controllers? I get errors of: Unable to find template (Lookeed into: project/templates). In this tutorial I dont think we told symfony to look for the twig templates in the old app bundles. Would it require removing all the @template annotations and changing to rendering the full path to the template instead for all the app bundle controllers? If that is the case probably just better off to finish moving to the flex structure.

Reply

Hey Michael L.!

Hmm, that's a good question - I'm not directly familiar because I don't use @Template. But yes, as soon as AppBundle is gone... then it would start looking for controllers in project/templates. My first instinct is the same as your's: finish moving those templates to their new location - that might be the easiest path. This upgrade can be painful - keep it up! It won't be repeated again - the upgrade from 4 to 5 is super easy.

Cheers!

Reply
Levure Avatar

Hello !
At 2'2 you explain to go to https://github.com/symfony/... and copy the console/bin content.

When I go to that URL, I have two folders : 3.3 and 4.4. Which one should I pick since I use Symfony 4.3.5 ?

By the way, huge thanks for the subtitles. As a deaf 🧏🏻‍♂️ developer, It helps me a lot !
Keep up the amazing work !

Reply

Hey Levure

I'm so glad to hear that subtitles are really useful to you :) (I'm who implemented it)

About your question. I think you can just use the code from 4.4, give it a try and if it doesn't work let me know

Cheers!

Reply
Daniel L. Avatar
Daniel L. Avatar Daniel L. | posted 3 years ago

Hi, I followed the tutorial, but public/index.php , src/Kernel.php , config/services.yaml ... are missing. any idea why ?

Reply

Hey Daniel L.!

Hmm. The recipe from symfony/framework-bundle should have given you these files. It makes me think that recipe didn't run. First thing to try: empty your vendor directory and run "composer install" - that might trigger that recipe to execute, if it didn't for some reason.

Cheers!

Reply
Daniel L. Avatar

ok, I'll give a try tomorrow ! Thx, awesome videos btw !

Reply
Daniel L. Avatar

Seems to work by deleting vendor/ and re-install ! Thx a lot

Reply
Default user avatar

I am getting following error while using bin/console cache:clear

In FileLoader.php line 168:

The autoloader expected class "App\MyApp\AccessPointDataBundle\Admin\AccessPointDataAdmin" to be defined in file "/var/www/Symfony3to4/vendor/composer/../../src/
MyApp/AccessPointDataBundle/Admin/AccessPointDataAdmin.php". The file was found but the class was not in it, the class name or namespace probably has a typo in /
/var/www/Symfony3to4/config/services.yaml (which is loaded in resource "/var/www/Symfony3to4/config/services.yaml").

In DebugClassLoader.php line 200:

The autoloader expected class "App\MyApp\AccessPointDataBundle\Admin\AccessPointDataAdmin" to be defined in file "/var/www/Symfony3to4/vendor/composer/../../src/
MyApp/AccessPointDataBundle/Admin/AccessPointDataAdmin.php". The file was found but the class was not in it, the class name or namespace probably has a typo.

I am not able to find any solution, please help.
Composer.json :

"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/"
}
},
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"doctrine/data-fixtures": "^1.3",
"doctrine/doctrine-bundle": "^1.6",
"symfony/console": "*",
"symfony/flex": "^1.1",
.....

config/services.yaml
parameters:
locale: 'en'
cache_type: file_system

services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means

App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,AppBundle,Kernel.php}'

App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']

AppBundle\:
resource: '../src/AppBundle/*'
exclude: '../src/AppBundle/{Entity,Repository,Tests}'

AppBundle\Controller\:
resource: '../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']

Reply

Hey Disha

Is probably that the namespace of such class it's wrong. Could you double check it?
Oh, and what's inside "MyApp" folder? because I see you also have "AppBundle"

Cheers!

Reply
Default user avatar

It was working correctly with symfony 3.4, do we have to change namespace for all the classes for 4.1 upgrade?
MyApp is a folder in which all my application bundles are. like src/MyApp/LocationBundle, src/MyApp/MediamanagerBundle, src/MyApp/EventBundle ect. AppBundle is at src/AppBundle.

Inside : AccessPointDataAdmin.php
namespace MyApp\AccessPointDataBundle\Admin;
please help.

Reply

If you don't want to migrate all your bundles then you will have to exclude them from being auto-registered as you did to the AppBundle:


exclude: '../src/{DependencyInjection,Entity,Migrations,Tests, *AppBundle*, Kernel.php}'
Reply
Kaizoku Avatar
Kaizoku Avatar Kaizoku | posted 4 years ago

If you have more bundles than the demo app you might get errors likes :

The child node "db_driver" at path "fos_user" must be configured.

I suppose, this is because the bundle don't provide a config file, so at the moment to stick with the course I just commented those bundle in config/bundles.php

Reply

Hey Kaizoku ,

You're right, because the recipe is not yet available. Some bundles still have no release tag for Symfony 4 support like FOSUserBundle. If you use this bundle, you can point to dev-master in composer.json for it and also check the original PR: https://github.com/FriendsO... - you'll find some temporary workarounds.

Cheers!

Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | posted 4 years ago

I think I messed up things with git checkouts between 3.4 and 4.0... For example the src/Controller directory is missing... how can I reconfigure flex?

Reply

Hey Trafficmanagertech ,

What do you mean about reconfiguring Flex? Did you require Flex with Composer as we show in this chapter? You can manually create src/Controller directory and move there all your controllers from the old src/AppBundle/Controller.

Cheers!

1 Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | victor | posted 4 years ago

Yes, at the end I started over... Anyway I have another error, "ClassNotFoundException: Attempted to load class "SensioDistributionBundle" from namespace "Sensio\Bundle\DistributionBundle"".
Do I need to remove the Sensio lines from the AppKernel.php?

Reply

Hey Trafficmanagertech ,

Hm, that's the correct namespace, see: https://github.com/sensiola... . Please, make sure you have installed "sensio/distribution-bundle", I think you can check it with Composer:
composer info sensio/distribution-bundle

If it's not installed, then you need to comment it out in AppKernel or remove this line from there at all.

Cheers!

Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | victor | posted 4 years ago

I deleted it from composer.json in the episode 3, as he said

Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | Trafficmanagertech | posted 4 years ago

...after removing it from bundles.php (I see it's not present in his finished project folder) it worked. sorry for so many questions :))

Reply

Great! I'm glad you for it working!

And don't worry about many questions ;)

Cheers!

1 Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | victor | posted 4 years ago

Actually it didn't work completely :( there is still an error: http://fm0.it/public/tm/sf4...

Reply

Hey Trafficmanagertech

Looks like something else is trying to require that class and by looking at your screenshot I can see it comes from a cached class; try removing entirely your var/cache/dev folder

$ rm var/cache/dev/*

Cheers!

1 Reply
Trafficmanagertech Avatar
Trafficmanagertech Avatar Trafficmanagertech | MolloKhan | posted 4 years ago

It didn't work, I fixed it with a `composer require sec-checker`, but honestly I don't know why it was needed, I never used that library...

Reply

Hey Trafficmanagertech ,

Hm, probably you have a dependency which requires sec-checker, but in this case "composer update" had to download it for you, so difficult to say something more without deeper debugging.

Anyway, glad you fixed and this error ;)

Cheers!

Reply

OK, so if you deleted it - then yes, remove it from AppKernel too otherwise it will cause errors ;)

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// 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
    }
}