10.09.19

Introduction about Symfony Flex

5/5 - (6 votes)

I. Introduction

  • Symfony Flex is a Composer plugin that modifies the behavior of the requirements, update, and remove commands.
  • Symfony Flex is the new way to install and manage packages
  • Auto-configuration via recipes
  • Flex works for Symfony 3.3 or higher, from Symfony 4.0 Flex is used as an option by default.

II. How does Flex work?

When installing new package by execute “composer require”, the app will request to the Symfony Flex server first:

  • If there’s no information about that package, the Flex server returns nothing and the package installation follows the usual procedure based on Composer;

  •   If there’s a special information about that package, Flex returns it in a file called a “recipe” and the application uses it to decide which package to install and which automated tasks to run after the installation.

III. Symfony Flex recipes

Symfony Flex recipes manage all third-party packages to integrate into Symfony application.

Two repositories:

  • Symfony/recipes

– Maintained by Symfony Core Team

– Can use alias

  • Symfony/recipes-contrib

– Anyone can contribute

– Cannot use alias

Recipes are defined in “mainfest.json” file.

Example mainfest.json of swiftmailer package

[pastacode lang=”markup” manual=”%7B%0A%20%20%20%20%22bundles%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22Symfony%5C%5CBundle%5C%5CSwiftmailerBundle%5C%5CSwiftmailerBundle%22%3A%20%5B%22all%22%5D%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22copy-from-recipe%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22config%2F%22%3A%20%22%25CONFIG_DIR%25%2F%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22env%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22MAILER_URL%22%3A%20%22smtp%3A%2F%2Flocalhost%3A25%3Fencryption%3D%26auth_mode%3D%22%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22aliases%22%3A%20%5B%22mailer%22%2C%20%22mail%22%2C%20%22swiftmailer%22%5D%0A%7D” message=”Example mainfest.json of swiftmailer package” highlight=”” provider=”manual”/]

Install new package

[pastacode lang=”markup” manual=”src%20git%3A(master)%20%E2%9C%97%20composer%20require%20mailer” message=”Install new package” highlight=”” provider=”manual”/]

Result: we can see Flex  auto-configuration, we don’t need to look or edit bundles.php.

[pastacode lang=”markup” manual=”return%20%5B%0A%20%20%20%20Symfony%5CBundle%5CFrameworkBundle%5CFrameworkBundle%3A%3Aclass%20%3D%3E%20%5B’all’%20%3D%3E%20true%5D%2C%0A%20%20%20%20Symfony%5CBundle%5CSwiftmailerBundle%5CSwiftmailerBundle%3A%3Aclass%20%3D%3E%20%5B’all’%20%3D%3E%20true%5D%2C%0A%5D%3B” message=”Result” highlight=”” provider=”manual”/]

Thus, Symfony Flex is so convenient. It helps you improve your productivity so much and create Symfony applications, from the most simple micro-style projects to the more complex ones with dozens of dependencies. We strongly recommend to upgrade your existing applications to it. 

Do you have project in mind? Do not hesitate to contact us! 😉

About Cuong Nguyen Van