Buy
Buy

Your Flex Project is Alive!

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!

  • 2018-02-28 nospor

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

  • 2018-02-28 weaverryan

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

  • 2018-02-28 the_nuts

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

  • 2018-02-28 nospor

    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...

  • 2018-02-27 weaverryan

    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!

  • 2018-02-27 nospor

    Was it connected somehow to twig?

  • 2018-02-27 the_nuts

    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

  • 2018-02-27 nospor

    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 :(

  • 2018-01-22 Victor Bocharsky

    Hey the_nuts ,

    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!

  • 2018-01-19 the_nuts

    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...

  • 2018-01-19 Diego Aguiar

    Hey the_nuts

    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!

  • 2018-01-18 the_nuts

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

  • 2018-01-18 Victor Bocharsky

    Great! I'm glad you for it working!

    And don't worry about many questions ;)

    Cheers!

  • 2018-01-18 Victor Bocharsky

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

    Cheers!

  • 2018-01-18 the_nuts

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

  • 2018-01-18 the_nuts

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

  • 2018-01-18 Victor Bocharsky

    Hey the_nuts ,

    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!

  • 2018-01-18 the_nuts

    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?

  • 2018-01-18 Victor Bocharsky

    Hey the_nuts ,

    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!

  • 2018-01-18 the_nuts

    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?

  • 2017-12-28 Victor Bocharsky

    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!

  • 2017-12-27 kaizoku

    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