Symfony is indeed a popular PHP framework. With 50M monthly downloads and a strong community is really an option to consider starting your next project with Symfony 4. What I'll try to show you in this article is answering in questions about how to create your first Symfony 4 project, what have changed from past versions, some step to step explanations about some new features.
What's new in this version?
The biggest change is given by "Micro by default". When do we compare with Symfony 3, at the very first step after the installation, the amount of required packages is way lighter! If before the developers were complaining about removing twig, doctrine or swift mailer bundles, now they have only what is really necessary! This is good. As Silex framework by reach end of life at June 2018, the chance of starting small and grow as the project need is proven to be very reliable on this new Symfony version.
After changing the way how the framework is built there are, at least, two more topics that worth to mention. First, there are no bundles by default. Until Symfony 3 we could create separate bundles and work individually with them, now we are developing our application and the architecture is defined by us. Could we do this also before? Yes, sure, inside the default AppBundle. Now on, you need just to create your logic inside your App namespace.
Second, Symfony Flex. This new player is proving to be an awesome creation from SensioLabs. Basically Symfony Flex helps to install and configure project dependencies using recipes. These recipes contain steps to be executed when you manage your dependencies with composer, defining configuration files and other stuff. A great example is, when you require to composer the Doctrine Bundle, you no longer need to add the extra line on AppKernel.php file or the extra configurations at the yaml files as well. Of course, if you are new at this Symfony 4 I guess you no need to worry about this. Please find more details about official and contributors Symfony flex recipes at www.symfony.sh
Create your first (or not...) Symfony 4 project
We no need Symfony Installer anymore! Now, creating a Symfony 4 project relies only on Composer commands. So, do you have Composer and PHP 7.1 installed? Execute then: https://gist.github.com/d029c5160822d9d018cb3cd2b9b9a1c6
Please check the output: https://gist.github.com/9e76b39850654d7e25314bad53efc68d
Understanding the output
After Composer finishes his work, we can check that three different tasks were done.
The first task is to clone a skeleton structure. After, composer will do the usual task of getting all
Finally, Symfony Flex takes in action. As a Composer plugin Flex will execute all recipes from the installed dependencies. As a hint, a message is presented to tell you how to fire up locally your application and suggests you to install a new dependency (we'll cover that later).
The project is created, What's next?
After the project is done, and before fire it up to browse it, there's some things to note.
One cool "feature" is that a git repository is automatically created. Change your directory to the project's directory and: https://gist.github.com/c7de5e0b0e4c5356f0c2926474ed4b6c
In order to keep track of our changes in this article I'll commit this project, so do not forget to add our remote origin if you want to.
With this new version, and looking for the skeleton we really can find a minimal, but powerful version in terms of scalability. Let's look for the folder directory: https://gist.github.com/885ae39114a110b2af349761d5c35f7c
There's some changes from the past versions in terms of file structure.
In this folder you can find application extra helpers.
console (Symfony Console component) can be found in this directory to help us to execute commands (we will cover this later) and also a
phpunit executable if you require that dependency.
A new folder not seen before. What it was a
web/ folder is now a
public/ folder. This is the document root for your web application.
Another change at this level is around the
FrontControllers, not having anymore the
app.php FrontController or
app_dev.php FrontController. This way
index.php is the only FrontController and the environment is handled by configurations.
This is one of the folders that the developer will handle. All service definitions, parameters, routes and bundle configurations are defined here. So if you want to define your services for the container you will make it at the
services.yaml file and (if not using annotations) manage routes in the
An important topic is the Environment oriented development. Symfony 4 relies now on Environment Variables for some configuration level. The
APP_ENV variable will define if your application behaves as
prod, or another folder you create inside
packages/ folder, and yes, to create an application environment you only have to create a folder inside
packages/ folder. To choose which environment to run change the environment variable in the
.env file created in the project directory.
It's a well known code folder. In this folder you will create your application's logic.
A small change from previous Symfony version is the
kernel.php file. As Symfony 4 is no longer a bundle oriented framework this
kernel.php will act as your bootstrap logic triggering
Routes configuration, but also to let you define later your compiler pass logic overriding
There's not too much to say about this folder. Here Symfony will store cache and logs.
Installing your first dependency
Let's have a better look for Symfony Flex actions when we require a new dependency.
Again, Symfony Flex is a Composer plugin, this means that adds some behaviour to Composer (nothing new in here). So as the
composer create-project has suggested, let's require the dependency
server. Please note that there's no vendor name on this require like
<vendor-name>/<package-name> and this happens thanks to the
alias composer feature.
If you search at [http://www.symfony.sh](Symfony Flex) for the
server recipe you will find that server in one of the alias for this Symfony Bundle.
Understanding the output
Obviously Composer starts by downloading the packages, then Symfony Flex will check for any recipes and execute them (what in this case happened for one recipe). What if we check git status? https://gist.github.com/275554394eea2bed116bb5de047bf864
composer.* files will store dependencies data,
symfony.lock will act has the
composer.lock file regarding executed recipes and
bundles.php enables this new required bundle in the application.
The Symfony Console
So far, we created a new project and required the Symfony
server dependency. This dependency is an extended way to use the PHP built-in server to allow you start a web server while developing locally.
How can you use it as other useful commands? Using Symfony Console, that one in the
Calling console component like above, you will see the full options list of what you can do with it. You should see a list similar to: https://gist.github.com/5aa6231d3fe504c5e9723779a0c3c2cc
You can pass as an option the name of the command you want to execute. For instance: https://gist.github.com/d3c4f79307d88310db970205a9f35269
This command will clear your application's cache.
So did you realise already about the
With this specific options you can run synchronously the web server or simply start it asynchronously: https://gist.github.com/641bd7df9bd311811b5ddf422d22b436
What you could get from this article:
- Create a Symfony 4 Project
- Read the basic new features and what changed from Symfony 3 to 4.
- Add dependencies and see Symfony Flex working
- Browse your first (or not) Symfony 4 application