12 \ August 2019 \ http://www.phparch.com
Symfony 4: A New Way to Develop Applications
There are a few things you need to
consider when using autowiring, read
more at adayinthelifeof.nl^20.
- They do not work on scalar type
hints, only objects. Meaning you
cannot autowire strings, ints, or
Booleans, for example. Symfony
automatically throws an exception
if it runs into this. - Contrary to many people’s beliefs,
autowiring does not impose a
performance hit since all services
are eventually compiled into a
cacheable container file. However,
when in dev mode, it might recom-
pile more often when you modify
classes, but unless you have very
large projects, that should not be a
problem. - There is absolutely no need to use
autowiring. Everything done with
autowiring can be done by manual-
ly defining services too. Autowiring
removes the need to write large
service files with boilerplate. You
are not missing out on anything
when you opt for not using autow-
iring. - Autowiring is not magic. It’s just
Symfony trying to automatically
inject services with other services
based on their fully qualified class
name. - Marking our services private by
default makes your app faster! It’s
not huge, but private services are
faster than public services
Symfony Webpack Encore
We notice in previous years that
frontend technologies are moving quite
fast, and in Symfony 3 AsseticBundle
for handling JS and CSS was not the
best solution. It was not very easy or
developer-friendly, so usually, devel-
opers set up some other solutions to
use. Webpack Encore to the rescue. It
allows us to integrate Webpack in our
Symfony application. It wraps Webpack
and gives us one additional layer on top
20 adayinthelifeof.nl: https://phpa.me/adayinthelife-autowire
21 frontend documentation: https://symfony.com/doc/current/frontend.html
22 release announcement: https://phpa.me/symfony-4-3-release
of it, giving us the ability to use it more
easily and increasing DX. To use it in a
project, you need to run:
composer require webpack-encore
You get the folder where your assets
go—./assets—and ./webpack.config.js
is where your configuration goes. You
can see an example in Figure 11.
Webpack Encore is very powerful;
optimizing and minimizing CSS and JS
files is just one of the features; for more
examples, check the frontend docu-
mentation^21.
You can use it for non-Symfony
applications also:
yarn add \@symfony/webpack-encore --dev
Updating to Symfony 4
Upgrading to Symfony 4 should not
be a problem. There is a little work,
like cleaning code, mostly service defi-
nitions (I hope you have tests). If you
are using Symfony 2, you should first
upgrade to Symfony 3.4 then move to
Symfony4. Why? Because they have the
same set of features—Symfony 4 is the
clean version with Flex. You need to
reorganize the folders and code a little
bit, and that takes some time, but it’s
worth it.
Symfony has a very well defined
release process, so in May 2019 they
released version 4.3, which is not an
LTS version. Even though there is no
Symfony 4 LTS version released yet, it
is worth upgrading to Symfony 4. You
can check the complete list of features
included in 4.3 in the release announce-
ment^22.
One Year After
I started using Symfony 4 a year ago,
and I got used to Symfony Flex and
autowiring quite quickly. I started with
a small project, building a web scraper
which grows in a matter of architecture
and complexity quite fast. However, I
Figure 11