phparchitect-2019-08

(Rick Simeone) #1
http://www.phparch.com \ August 2019 \ 5

Symfony 4: A New Way to Develop Applications

Symfony Flex FTW!


Let’s first talk a little bit about
Symfony Flex^7 , a Composer plugin. It
can be required with one command.


composer require symfony/flex


It’s a real plug-and-play feature for
Symfony and Composer. Flex installs
the package you want and does some
additional things. Typically, these are
the steps you needed to do manually in
the past. Symfony Flex was the compo-
nent that was missing. Composer
should have been a tool that allowed
you to easily add or remove bundles
(like plugins) from your application.
However, we were doing all of these
steps manually—adding configurations,
removing configurations, etc. With
Symfony Flex, we don’t need to do this,
the goal is to have zero-configuration,
and it works.


Symfony Flex is a Composer plugin
that modifies the behavior of the
require, update, and remove commands.
When installing or removing depen-
dencies in a Flex-enabled application,
Symfony can perform tasks before
and after the execution of Composer
tasks. Symfony Flex plays a huge role in
providing better DX. As I stated before,
and we see later, you start with almost
nothing, and you only add things
when needed. Symfony Flex allows us
to add third-party bundles as plug-
and-play plugins (add if you needed it
and remove it when you don’t need it)
without any additional configuration
(Figure 2).


After you run composer require,
Symfony Flex asks Symfony Flex
server^8 if there is a recipe or any addi-
tional information about the installed
package. If there is no information or
no recipe, nothing happens, and you do
the configuration manually as before. If
recipes exist, the server answers with
yes and sends the recipes. Composer
reads the recipes and executes all the
steps defined inside the recipe.


7 Symfony Flex: https://flex.symfony.com
8 Flex server:
https://phpa.me/symfony-setup-flex


In Listing 1, we can see a simple
recipe for SwiftmailerBundle^9. On the
first line, it tells Symfony Flex how to
enable the bundle in the configuration.
The copy-from-recipe line specifies
which configuration files need to be
copied to the Symfony project config-
uration. Later, we discuss the new
directory structure and a new way of
configuration in Symfony 4. There
are two repositories related to recipes.
The Symfony core team maintains a
Symfony/recipes^10 repository, and they
guarantee the quality of the compo-
nents. Only these recipes can use an
alias. An alias is a name which you
can use with Composer require, so in
this case composer require mailer or
composer require mail give the same

9 SwiftmailerBundle:
https://phpa.me/swiftmailer-manifest
10 Symfony/recipes:
https://github.com/symfony/recipes

results. Symfony.sh is the site where you
can get a list of all recipes.

Starting a New Project With
Symfony 4
There are a few ways to start a
new Symfony project. You can use
Composer, or you can use the Symfony
client^11. The Symfony client is a potent
tool, and I recommend using it. It helps
you:


  1. Create new Symfony applications.
    2. Provide a local HTTP/2 web server.
    3. Generate TLS certificates.
    4. Check for security vulnerabilities.
    5. Seamlessly integrate with
    SymfonyCloud^12.


11 Symfony client:
https://symfony.com/download
12 SymfonyCloud:
https://symfony.com/cloud/

Listing 1


  1. {

  2. "bundles": {

  3. "Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle": ["all"]

  4. },

  5. "copy-from-recipe": {

  6. "config/": "%CONFIG_DIR%/"

  7. },

  8. "env": {

  9. "#1": "For Gmail as a transport, use:
    \"gmail://username:password@localhost\"",

  10. "#2": "For a generic SMTP server, use:
    \"smtp://localhost:25?encryption=&auth_mode=\"",

  11. "#3": "Delivery is disabled by default via \"null://localhost\"",

  12. "MAILER_URL": "null://localhost"

  13. },

  14. "aliases": ["mailer", "mail"]

  15. }


Figure 2
Free download pdf