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!

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