A dive into
Symfony 4
A long time ago...
Symfony 2 was born
Key Concepts
Components

Bundles

Distributions
Time passed by...
How did it go?
Bene... ma non benissimo :-(
Bene... ma non benissimo :-(
Distributions
Initial idea: one distribution per app type

• Standard Distribution

• Rest Distribution

• Cms Distribution

• ...
Distributions
In Reality: distributions never took off, everybody uses
Standard Distribution and removes stuff manually
Bundles
Initial Idea: organise reusable code in
bundles
Bundles
Have you ever seen this organisation?
ShopBundle

CmsBundle

AdminBundle

...
Bundles
Have you ever seen this organisation?
ShopBundle

CmsBundle

AdminBundle

Common|General| ... | Bundle
Bundles
Vendor code is reusable

Your App code is not
Bundles
Installing/Removing a Bundle is mostly a manual operation

# composer require/remove <bundle>

# add/remove to AppKernel.php

# add/remove configurations
DX initiative
Symfony has a steep learning curve compared to other
frameworks 

The Developer eXperience initiative aims to create a
better onboarding and framework usage experience
DX initiative
Symfony has a steep learning curve compared to other
frameworks 

The Developer eXperience initiative aims to create a
better user experience
Symfony 4 was born...
New Directory Structure
New Directory Structure
The idea behind that is to have a more
intuitive structure
New Directory Structure
bin

config

public 

src

var

vendor
New Directory Structure
bin

config

public 

src

var

vendor
index.php
...
favicon.ico
robots.txt
bundles
New Directory Structure
bin

config

public 

src

var

vendor
cache/
log/
session/
New Directory Structure
bin

config

public 

src

var

vendor
Kernel.php
Entity
Repository
Command
Controller
...
No Bundles!
New Directory Structure
bin

config

public 

src

var

vendor
bundles.php

routes.yaml

services.yaml

doctrine.yaml

security.yaml

...

packages/

dev/
routes.yaml

test/
routes.yaml

prod/
routes.yaml
return [

SymfonyBundleFrameworkBundleFrameworkBundle::class => ['all' => true],

SymfonyBundleWebServerBundleWebServerBundle::class => ['dev' => true],

SymfonyBundleMakerBundleMakerBundle::class => ['dev' => true],

SymfonyBundleDebugBundleDebugBundle::class => ['dev' => true, 'test' => true],

SymfonyBundleSecurityBundleSecurityBundle::class => ['all' => true],

];
New Directory Structure
assets

bin

data

config

public 

src

templates

tests

translations

var

vendor
Where is parameter.yaml?
Environment Variables
Environment Variables
Symfony already has them eg.
SYMFONY__ENV
Environment Variables
Now resolved at runtime! You can change
them without touching the app

https://www.12factor.net/config
Environment Variables
doctrine:

dbal:

driver: 'pdo_sqlite'

server_version: '3.15'

charset: utf8mb4

url: ‘%env(resolve:DATABASE_URL)%'


https://symfony.com/blog/new-in-symfony-3-4-advanced-environment-variables
.env
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=2a0f335581bd72b6077840e29d73ba36
TRUSTED_PROXIES=127.0.0.1,127.0.0.2
TRUSTED_HOSTS=localhost,example.com,127.0.0.1
###< symfony/framework-bundle ###
index.php
if (!isset($_SERVER['APP_ENV'])) {

if (!class_exists(Dotenv::class)) {

throw new RuntimeException('APP_ENV environment variable is not defined.’);

}



(new Dotenv())->load(__DIR__.'/../.env');

}
$env = $_SERVER['APP_ENV'] ?? 'dev';

$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));
Symfony Flex
Flex
A new way to create and evolve a Symfony application

Composition over inheritance

Start small and add what you need
Flex
Integrates into composer workflow 

Automates bundles installation and configuration
Flex
Flex
Flex
{
"bundles": {
"SymfonyBundleFrameworkBundleFrameworkBundle": ["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/"
]
}
https://medium.com/@fabpot/symfony-4-automate-your-workflow-fbbf609b5a1d
symfony.sh
recipes
https://github.com/symfony/recipes 

https://github.com/symfony/recipes-contrib
alias
orm, twig, security, form, validation, ...
So long Standard Edition...
composer require symfony/website-skeleton
composer require symfony/skeleton
So long Silex...
https://symfony.com/blog/the-end-of-silex
Other Bits...
From Sf 3.* to Sf 4
Symfony 3.4 is Symfony 4 with and additional compat layer

Migration path: upgrade to 3.4, remove deprecations,
update to 4 

https://symfony.com/doc/current/setup/flex.html#upgrade-to-flex
Autowiring
Wait 5 minutes... :-P
composer require make
MakerBundle
make:auth

make:command

make:controller

make:crud

make:entity

make:fixtures

make:form

make:functional-test

make:migration

make:serializer:encoder

make:subscriber

make:twig-extension

make:unit-test

make:user

make:validator

make:voter
Fastest PHP Router
https://symfony.com/blog/new-in-symfony-4-1-fastest-php-router
VarDumper Server
https://symfony.com/blog/new-in-symfony-4-1-vardumper-server
Messenger Component
https://symfony.com/blog/new-in-symfony-4-1-messenger-component
Panther
https://symfony.com/blog/introducing-symfony-panther-a-browser-
testing-and-web-scrapping-library-for-php
Moar Improvements
https://symfony.com/blog/new-in-symfony-4-1-exception-improvements

https://symfony.com/blog/new-in-symfony-4-1-session-improvements

https://symfony.com/blog/new-in-symfony-4-1-self-updating-debug-toolbar

https://symfony.com/blog/new-in-symfony-4-1-console-improvements

https://symfony.com/blog/new-in-symfony-4-1-form-field-help

https://symfony.com/blog/new-in-symfony-4-1-faster-serializer

https://symfony.com/blog/new-in-symfony-4-1-misc-improvements-part-1

https://symfony.com/blog/new-in-symfony-4-1-misc-improvements-part-2

https://symfony.com/blog/new-in-symfony-4-1-misc-improvements-part-3

https://symfony.com/blog/new-in-symfony-4-1-misc-improvements-part-4

https://symfony.com/blog/new-in-symfony-4-2-samesite-cookie-configuration

https://symfony.com/blog/new-in-symfony-4-2-varexporter-component

https://symfony.com/blog/new-in-symfony-4-2-intlmessageformatter

https://symfony.com/blog/new-in-symfony-4-2-auto-secure-cookies
Thank you!
Michele Orselli
_orso_
micheleorselli / ideatosrl
mo@ideato.it
https://joind.in/talk/2a90f

A dive into Symfony 4