Symfony4: a new way to develop
applications
Antonio Perić-Mažar, Locastic
26.05.2019 - #phpsrb
Antonio
Perić-Mažar
CEO @ Locastic
Co-founder @ Tinel Meetup
Co-founder @ Blockada
t: antonioperic
m: antonio@locastic.com
Locastic
Helping clients create web and mobile apps since 2011
• UX/UI
• Mobile apps
• Web apps
• Training & Consulting
www.locastic.com
@locastic
2005.
php 5.3
2009.2005.
php 5.3
2009.2005. 2015.
php 5.3
2009.2005. 2015. Nov 2017.
• Installing bundle is too cumbersome
• Remove bundle is too cumbersome
• The standard edition is not good enough
• You always had a feeling that you have a bunch of code and
packages that your are not using
What we can do better?
– Fabien Potencier
“As a developer, I want to start small, without too many
dependencies. But I also want to be able to grow my
application as I see fit. From a micro-framework style app to a
gigantic monolith. Your choice. The framework should not get
in the way.”
Composition over
inheritance
• Starting as micro framework
• Compose your application
• Build anything you want; console app, traditional web app, etc
• Based on a micro-kernel and contain 70% less code and files than
new Symfony 3 apps
Symfony4
Composer started as a conversation about how to generically install
bundles/plugins/extensions for Symfony and phpBB.
Fun fact
Composer started as a conversation about how to generically install
bundles/plugins/extensions for Symfony and phpBB.
Fun fact
Neither Symfony nor phpBB uses Composer as a way to install its
bundles/plugins/extensions.
WTF fact
• Composer plugin
• Auto-configurable via recipes
• Official and private recipes
Symfony Flex
Decide which package to install
Run any task to configure them
{
"bundles": {
"SymfonyBundleSwiftmailerBundleSwiftmailerBundle":
["all"]
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/"
},
"env": {
"#1": "For Gmail as a transport, use: "gmail://
username:password@localhost"",
"#2": "For a generic SMTP server, use: "smtp://localhost:
25?encryption=&auth_mode="",
"#3": "Delivery is disabled by default via "null://
localhost"",
"MAILER_URL": "null://localhost"
},
"aliases": ["mailer", "mail"]
}
Symfony mailer recipe
• Two repositories
• symfony/recipies
• maintained by Symfony core team, contains only recipies for
components and bundles ‘opinionated’ by core team
• can use alias
• symfony/recipies-contrib
• anyone can contribute
• cannot use alias
Symfony Flex Recipies
https://flex.symfony.com/
• With composer
• composer create-project symfony/skeleton
• composer create-project symfony/website-skeleton
• composer create-project symfony/demo
Starting a new project
• With Symfony Client
• Helps you create new Symfony applications
• supports multiple PHP versions, different PHP config per directory
• Provides a local HTTP/2 web server
• Generates TLS certificates
• Checks for security vulnerabilities
• Seamlessly integrates with SymfonyCloud
• Works on Windows also
Starting a new project
• With Symfony Client
• symfony new my_project
• symfony new ——full my_poject
• symfony serve
Starting a new project
Configuration
No more different
controllers for different enviroments
Your code goes
here
Cache, log…
vendors
.env variables
symfony.lock is used
by flex to track which recipies are
installed in the project
• parameters.yaml is gone
• more flexible but still you need to care about security
• .env - commit - defaults
• .env.local - gitignore
• .env.test - commit - defaults
• .env.test.local - gitignore
.env
Don’t put secrets in .env
file!!!
• Bundle vs no-bundle apps
• All in src/ folder
• App/ namespace
• You should separate, but no need for bundles
• Moving forward to standardisation
• Reduces the perceived complexity, makes your code feels more
decoupled from symfony
• Bundle inheritance mechanisms are depricated in 3.4, removed in 4.0
Bundle-less
applications
Say hi to autowiring
• Introduced in Symfony3
• Allows you to manage services in the container with minimal
configuration
• Reads type-hints on your constructor (or other methods) and
automatically passes the correct services
• Designed to be predictable; if it is not absolutely clear which
dependency should be passed, you will see actionable exception
Autowiring
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
AppController:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
By default, all services
are now private.
$container->get(‘service_name’)
is dead
<?php
namespace AppService;
use AppUtilRot13Transformer;
class TwitterClient
{
private $transformer;
public function __construct(Rot13Transformer $transformer)
{
$this->transformer = $transformer;
}
public function tweet($user, $key, $status)
{
$transformedStatus = $this->transformer->transform($status);
// ... connect to Twitter and send the encoded status
}
}
bin/console debug:autowiring
• Webpack Encore is a simpler way to integrate Webpack into your
application
• Step forward after AsseticBundle
• composer require webpack-encore
• Works outside of Symfony
Symfony Webpack
Encore
• Symfony on PHP 7.2 makes your code quite a bit faster than 7.1
• Symfony 2.3 is the second fastest Symfony release since 2.0
• Symfony 3.4 is the slowest release since 2.0 (deprecated features are
probably one of the reasons);
• Symfony 4.0 is almost three times faster as Laravel 5.5.
Performance
Stability and
predictability
• It is possible, not so complex (depending on project)
• It will take a little bit of time
• Upgrade first to Symfony 3.4
Updating to Symfony4
Keep cooding features!
Symfony4 spirit
• It is a great feeling working with Symfony4
• Symfony has great community that is moving framework forward
• Flex is amazing thing
• New folder structure is more organised and more natural
• Better standardisation
A year later
• Symfony is great as microframework
• Higher level of abstraction is better for prototyping and building
things faster
• Very easy to start, but maybe new developers will think there is to
much magic
• Symfony as framework should not be a limitation for you
A year later
Thank you!
Questions?
Antonio Perić-Mažar
t: antonioperic
m: antonio@locastic.com

Symfony 4: A new way to develop applications #phpsrb

  • 1.
    Symfony4: a newway to develop applications Antonio Perić-Mažar, Locastic 26.05.2019 - #phpsrb
  • 2.
    Antonio Perić-Mažar CEO @ Locastic Co-founder@ Tinel Meetup Co-founder @ Blockada t: antonioperic m: antonio@locastic.com
  • 3.
    Locastic Helping clients createweb and mobile apps since 2011 • UX/UI • Mobile apps • Web apps • Training & Consulting www.locastic.com @locastic
  • 7.
  • 8.
  • 9.
  • 10.
  • 12.
    • Installing bundleis too cumbersome • Remove bundle is too cumbersome • The standard edition is not good enough • You always had a feeling that you have a bunch of code and packages that your are not using What we can do better?
  • 13.
    – Fabien Potencier “Asa developer, I want to start small, without too many dependencies. But I also want to be able to grow my application as I see fit. From a micro-framework style app to a gigantic monolith. Your choice. The framework should not get in the way.”
  • 14.
  • 15.
    • Starting asmicro framework • Compose your application • Build anything you want; console app, traditional web app, etc • Based on a micro-kernel and contain 70% less code and files than new Symfony 3 apps Symfony4
  • 17.
    Composer started asa conversation about how to generically install bundles/plugins/extensions for Symfony and phpBB. Fun fact
  • 18.
    Composer started asa conversation about how to generically install bundles/plugins/extensions for Symfony and phpBB. Fun fact Neither Symfony nor phpBB uses Composer as a way to install its bundles/plugins/extensions. WTF fact
  • 19.
    • Composer plugin •Auto-configurable via recipes • Official and private recipes Symfony Flex
  • 21.
    Decide which packageto install Run any task to configure them
  • 22.
    { "bundles": { "SymfonyBundleSwiftmailerBundleSwiftmailerBundle": ["all"] }, "copy-from-recipe": { "config/":"%CONFIG_DIR%/" }, "env": { "#1": "For Gmail as a transport, use: "gmail:// username:password@localhost"", "#2": "For a generic SMTP server, use: "smtp://localhost: 25?encryption=&auth_mode="", "#3": "Delivery is disabled by default via "null:// localhost"", "MAILER_URL": "null://localhost" }, "aliases": ["mailer", "mail"] } Symfony mailer recipe
  • 23.
    • Two repositories •symfony/recipies • maintained by Symfony core team, contains only recipies for components and bundles ‘opinionated’ by core team • can use alias • symfony/recipies-contrib • anyone can contribute • cannot use alias Symfony Flex Recipies
  • 24.
  • 26.
    • With composer •composer create-project symfony/skeleton • composer create-project symfony/website-skeleton • composer create-project symfony/demo Starting a new project
  • 27.
    • With SymfonyClient • Helps you create new Symfony applications • supports multiple PHP versions, different PHP config per directory • Provides a local HTTP/2 web server • Generates TLS certificates • Checks for security vulnerabilities • Seamlessly integrates with SymfonyCloud • Works on Windows also Starting a new project
  • 28.
    • With SymfonyClient • symfony new my_project • symfony new ——full my_poject • symfony serve Starting a new project
  • 30.
  • 31.
    No more different controllersfor different enviroments
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
    symfony.lock is used byflex to track which recipies are installed in the project
  • 37.
    • parameters.yaml isgone • more flexible but still you need to care about security • .env - commit - defaults • .env.local - gitignore • .env.test - commit - defaults • .env.test.local - gitignore .env
  • 38.
    Don’t put secretsin .env file!!!
  • 39.
    • Bundle vsno-bundle apps • All in src/ folder • App/ namespace • You should separate, but no need for bundles • Moving forward to standardisation • Reduces the perceived complexity, makes your code feels more decoupled from symfony • Bundle inheritance mechanisms are depricated in 3.4, removed in 4.0 Bundle-less applications
  • 40.
    Say hi toautowiring
  • 41.
    • Introduced inSymfony3 • Allows you to manage services in the container with minimal configuration • Reads type-hints on your constructor (or other methods) and automatically passes the correct services • Designed to be predictable; if it is not absolutely clear which dependency should be passed, you will see actionable exception Autowiring
  • 42.
    # This fileis the entry point to configure your own services. # Files in the packages/ subdirectory configure your dependencies. # Put parameters here that don't need to change on each machine where the app is deployed # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration parameters: services: # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App: resource: '../src/*' exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}' # controllers are imported separately to make sure services can be injected # as action arguments even if you don't extend any base controller class AppController: resource: '../src/Controller' tags: ['controller.service_arguments'] # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones
  • 43.
    By default, allservices are now private. $container->get(‘service_name’) is dead
  • 44.
    <?php namespace AppService; use AppUtilRot13Transformer; classTwitterClient { private $transformer; public function __construct(Rot13Transformer $transformer) { $this->transformer = $transformer; } public function tweet($user, $key, $status) { $transformedStatus = $this->transformer->transform($status); // ... connect to Twitter and send the encoded status } }
  • 45.
  • 46.
    • Webpack Encoreis a simpler way to integrate Webpack into your application • Step forward after AsseticBundle • composer require webpack-encore • Works outside of Symfony Symfony Webpack Encore
  • 49.
    • Symfony onPHP 7.2 makes your code quite a bit faster than 7.1 • Symfony 2.3 is the second fastest Symfony release since 2.0 • Symfony 3.4 is the slowest release since 2.0 (deprecated features are probably one of the reasons); • Symfony 4.0 is almost three times faster as Laravel 5.5. Performance
  • 50.
  • 51.
    • It ispossible, not so complex (depending on project) • It will take a little bit of time • Upgrade first to Symfony 3.4 Updating to Symfony4
  • 53.
  • 54.
    • It isa great feeling working with Symfony4 • Symfony has great community that is moving framework forward • Flex is amazing thing • New folder structure is more organised and more natural • Better standardisation A year later
  • 55.
    • Symfony isgreat as microframework • Higher level of abstraction is better for prototyping and building things faster • Very easy to start, but maybe new developers will think there is to much magic • Symfony as framework should not be a limitation for you A year later
  • 56.
  • 57.