Future Symfony 4.0 Release and Project Using Symfony Flex

  • Tutorial

November 30, 2017 Symfony 4.0 release will take place



image

The fourth version has a number of global changes, the main of which is the transition to Symfony Flex.

What is symfony flex?


This is a new approach to organizing symphony applications, based on “recipes” .
According to the developers, this should simplify the work with dependencies \ bundles \ packages and bring more automation.

There are 2 repositories with recipes:


The recipe is configured through manifest.json which contains a set of configurators and options.

Options


aliases
Used to indicate an alternate name for the recipe. For example, without using this option, the recipe is installed like this:

composer req acme-inc/acme-log-monolog-handler

By adding to manifest.json

{
    "aliases": ["acme-log", "acmelog"]
}

can be used

composer req acme-log

Configurators


A set of tasks that will be performed during the installation of the recipe.
bundles

Connection of one or several bundles with the ability to specify the environment.

{
    "bundles": {
        "Symfony\\Bundle\\DebugBundle\\DebugBundle": ["dev", "test"],
        "Symfony\\Bundle\\MonologBundle\\MonologBundle": ["all"]
    }
}

container
Adding container parameters to services.yaml, for example, locale:

{
    "container": {
        "locale": "en"
    }
}

copy-from-package
Copy folders or files from the package to the project:

{
    "copy-from-package": {
        "bin/check.php": "%BIN_DIR%/check.php"
    }
}

Available constants:
% BIN_DIR%,% CONF_DIR%,% CONFIG_DIR%,% SRC_DIR%% VAR_DIR%,% PUBLIC_DIR%

copy-from-recipe
Copying files and directories from the recipe itself:

"copy-from-recipe": {
    "config/": "%CONFIG_DIR%/",
    "src/": "%SRC_DIR%/"
}

env
Adding parameters to .env and .env.dist:

{
    "env": {
        "APP_ENV": "dev",
        "APP_DEBUG": "1"
    }
}

You can generate a random 16-bit string using % generate (secret)%

gitignore
Adds patterns to .gitignore:

{
    "gitignore": [
        ".env",
        "/public/bundles/",
        "/var/",
        "/vendor/"
    ]
}

post-install-output
Defines the content that will be shown after installing the recipe, should be defined in the post-install.txt file , each line is closed by PHP_EOL. Console colors / styles are
supported . Full example manifest.json for symfony / framework-bundle :



{
    "bundles": {
        "Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle": ["all"]
    },
    "copy-from-recipe": {
        "config/": "%CONFIG_DIR%/",
        "public/": "%PUBLIC_DIR%/",
        "src/": "%SRC_DIR%/"
    },
    "composer-scripts": {
        "make cache-warmup": "script",
        "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
    },
    "env": {
        "APP_ENV": "dev",
        "APP_DEBUG": "1",
        "APP_SECRET": "%generate(secret)%"
    },
    "gitignore": [
        ".env",
        "/public/bundles/"
        "/var/",
        "/vendor/"
    ]
}


Build your first Symfony Flex project


We will use the latest version available at the time of writing - “v4.0.0-RC1”

First we need composer .

Let's create a project:


composer create-project symfony/skeleton flex-test-project "v4.0.0-RC1"

Initially, only official recipes are available to us. To use third-party recipes, you must complete:

composer config extra.symfony.allow-contrib true

or add it yourself

"extra": {
        "symfony": {
          "allow-contrib": "true"
        }

in composer.json

To start, you can use the built-in web server in PHP or put web-server-bundle through its recipe:


composer req web-server
bin/console server:start 

Also, we need a profiler and generator, which is now called maker-bundle .
Profiler requires twig.


composer req twig
composer req web-profiler-bundle

And for the annotation generator, it may also be necessary to lower the minimum-stability requirements in * composer.json *.

 "minimum-stability": "dev"


composer req maker
composer req annotations
bin/console make:controller

Done.



You can use https://symfony.sh/ to search for recipes .
Major changes in the upcoming version .
Repository on github
Upgrade from 3. * to 4. *

Also popular now: