Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
2016calendar
Tips amp Tricks
Setting custom tableproperties namesusing built-in Doctrine naming strategy
namespace
use as
class
private
private
AppBundleEntity
DoctrineORMMapping ORM
ApiUsers
$apiToken
$createdAt
ORMTable
ORMColumn(type=rdquostringrdquo)
ORMColumn(type=rdquodatetimerdquo)
automatically translatedinto (database)api_token
automatically translated into (database)api_users
automatically translatedinto (database)created_at
Doctrine
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestreferencenamingstrategyhtml
appconfigconfigyml
doctrine
dbal
orm
naming_strategy
doctrineormnaming_strategyunderscore
January
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Form
Best Practices (controller related)
parameters
appus_shippingenabled
services
appformshipping
class
arguments
tags
type
true
AppBundleFormShippingType
- appus_shippingenabled
- formtype
Pass global settings to constructor
class extends
private
public function __construct
bool
ShippingType AbstractType
$usShipping
($usShipping)
$this usShipping ( ) $usShipping
-gt =
$form $this createForm(PaymentTypeclass $order [
$this generateUrl( )
])
= -gt
=gt -gt
=gt
lsquoactionrsquo rsquopayment_gwrsquo
lsquomethodrsquo lsquoPUTrsquo
Use ldquoactionrdquo and ldquomethodrdquo options in the controller
Use custom options to pass dynamic data to constructor
Donrsquot pass dynamic data to constructor
$defaultShipping $this getDefaultShipping($customer)
$form $this createForm(
PlaceOrderType($defaultShipping)
)
= -gt
= -gt
new
$form $this createForm(PlaceOrderTypeclass [
$defaultShipping
])
= -gt
=gt lsquodefault_shippingrsquo Use customoptions
February
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28
Doctrine
Improved WHERE IN
$categories
$queryBuilder $this
where( )
setParameter( $categories)
=
=
-gt
-gt
rsquomodelcategory IN (categories)rsquo
rsquocategoriesrsquo
Doctrine 25+
WHERE INsupports the useof ArrayCollection
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestchangelogmigration_2_5htmlquery-api-where-in-query-using-a-collection-as-parameter
$categories
$categoryIds []
($categories $category)
$categoryIds[] $category getId()
$queryBuilder $this
where( )
setParameter( $categoryIds)
=
=
= -gt
=
-gt
-gt
foreach as
rsquomodelcategory IN (category_ids)rsquo
rsquocategory_idsrsquo
Before Doctrine 25 Doctrine 25+
transform theArrayCollectioninto an array of Ids
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Setting custom tableproperties namesusing built-in Doctrine naming strategy
namespace
use as
class
private
private
AppBundleEntity
DoctrineORMMapping ORM
ApiUsers
$apiToken
$createdAt
ORMTable
ORMColumn(type=rdquostringrdquo)
ORMColumn(type=rdquodatetimerdquo)
automatically translatedinto (database)api_token
automatically translated into (database)api_users
automatically translatedinto (database)created_at
Doctrine
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestreferencenamingstrategyhtml
appconfigconfigyml
doctrine
dbal
orm
naming_strategy
doctrineormnaming_strategyunderscore
January
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Form
Best Practices (controller related)
parameters
appus_shippingenabled
services
appformshipping
class
arguments
tags
type
true
AppBundleFormShippingType
- appus_shippingenabled
- formtype
Pass global settings to constructor
class extends
private
public function __construct
bool
ShippingType AbstractType
$usShipping
($usShipping)
$this usShipping ( ) $usShipping
-gt =
$form $this createForm(PaymentTypeclass $order [
$this generateUrl( )
])
= -gt
=gt -gt
=gt
lsquoactionrsquo rsquopayment_gwrsquo
lsquomethodrsquo lsquoPUTrsquo
Use ldquoactionrdquo and ldquomethodrdquo options in the controller
Use custom options to pass dynamic data to constructor
Donrsquot pass dynamic data to constructor
$defaultShipping $this getDefaultShipping($customer)
$form $this createForm(
PlaceOrderType($defaultShipping)
)
= -gt
= -gt
new
$form $this createForm(PlaceOrderTypeclass [
$defaultShipping
])
= -gt
=gt lsquodefault_shippingrsquo Use customoptions
February
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28
Doctrine
Improved WHERE IN
$categories
$queryBuilder $this
where( )
setParameter( $categories)
=
=
-gt
-gt
rsquomodelcategory IN (categories)rsquo
rsquocategoriesrsquo
Doctrine 25+
WHERE INsupports the useof ArrayCollection
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestchangelogmigration_2_5htmlquery-api-where-in-query-using-a-collection-as-parameter
$categories
$categoryIds []
($categories $category)
$categoryIds[] $category getId()
$queryBuilder $this
where( )
setParameter( $categoryIds)
=
=
= -gt
=
-gt
-gt
foreach as
rsquomodelcategory IN (category_ids)rsquo
rsquocategory_idsrsquo
Before Doctrine 25 Doctrine 25+
transform theArrayCollectioninto an array of Ids
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
January
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Form
Best Practices (controller related)
parameters
appus_shippingenabled
services
appformshipping
class
arguments
tags
type
true
AppBundleFormShippingType
- appus_shippingenabled
- formtype
Pass global settings to constructor
class extends
private
public function __construct
bool
ShippingType AbstractType
$usShipping
($usShipping)
$this usShipping ( ) $usShipping
-gt =
$form $this createForm(PaymentTypeclass $order [
$this generateUrl( )
])
= -gt
=gt -gt
=gt
lsquoactionrsquo rsquopayment_gwrsquo
lsquomethodrsquo lsquoPUTrsquo
Use ldquoactionrdquo and ldquomethodrdquo options in the controller
Use custom options to pass dynamic data to constructor
Donrsquot pass dynamic data to constructor
$defaultShipping $this getDefaultShipping($customer)
$form $this createForm(
PlaceOrderType($defaultShipping)
)
= -gt
= -gt
new
$form $this createForm(PlaceOrderTypeclass [
$defaultShipping
])
= -gt
=gt lsquodefault_shippingrsquo Use customoptions
February
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28
Doctrine
Improved WHERE IN
$categories
$queryBuilder $this
where( )
setParameter( $categories)
=
=
-gt
-gt
rsquomodelcategory IN (categories)rsquo
rsquocategoriesrsquo
Doctrine 25+
WHERE INsupports the useof ArrayCollection
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestchangelogmigration_2_5htmlquery-api-where-in-query-using-a-collection-as-parameter
$categories
$categoryIds []
($categories $category)
$categoryIds[] $category getId()
$queryBuilder $this
where( )
setParameter( $categoryIds)
=
=
= -gt
=
-gt
-gt
foreach as
rsquomodelcategory IN (category_ids)rsquo
rsquocategory_idsrsquo
Before Doctrine 25 Doctrine 25+
transform theArrayCollectioninto an array of Ids
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Form
Best Practices (controller related)
parameters
appus_shippingenabled
services
appformshipping
class
arguments
tags
type
true
AppBundleFormShippingType
- appus_shippingenabled
- formtype
Pass global settings to constructor
class extends
private
public function __construct
bool
ShippingType AbstractType
$usShipping
($usShipping)
$this usShipping ( ) $usShipping
-gt =
$form $this createForm(PaymentTypeclass $order [
$this generateUrl( )
])
= -gt
=gt -gt
=gt
lsquoactionrsquo rsquopayment_gwrsquo
lsquomethodrsquo lsquoPUTrsquo
Use ldquoactionrdquo and ldquomethodrdquo options in the controller
Use custom options to pass dynamic data to constructor
Donrsquot pass dynamic data to constructor
$defaultShipping $this getDefaultShipping($customer)
$form $this createForm(
PlaceOrderType($defaultShipping)
)
= -gt
= -gt
new
$form $this createForm(PlaceOrderTypeclass [
$defaultShipping
])
= -gt
=gt lsquodefault_shippingrsquo Use customoptions
February
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28
Doctrine
Improved WHERE IN
$categories
$queryBuilder $this
where( )
setParameter( $categories)
=
=
-gt
-gt
rsquomodelcategory IN (categories)rsquo
rsquocategoriesrsquo
Doctrine 25+
WHERE INsupports the useof ArrayCollection
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestchangelogmigration_2_5htmlquery-api-where-in-query-using-a-collection-as-parameter
$categories
$categoryIds []
($categories $category)
$categoryIds[] $category getId()
$queryBuilder $this
where( )
setParameter( $categoryIds)
=
=
= -gt
=
-gt
-gt
foreach as
rsquomodelcategory IN (category_ids)rsquo
rsquocategory_idsrsquo
Before Doctrine 25 Doctrine 25+
transform theArrayCollectioninto an array of Ids
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
February
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28
Doctrine
Improved WHERE IN
$categories
$queryBuilder $this
where( )
setParameter( $categories)
=
=
-gt
-gt
rsquomodelcategory IN (categories)rsquo
rsquocategoriesrsquo
Doctrine 25+
WHERE INsupports the useof ArrayCollection
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestchangelogmigration_2_5htmlquery-api-where-in-query-using-a-collection-as-parameter
$categories
$categoryIds []
($categories $category)
$categoryIds[] $category getId()
$queryBuilder $this
where( )
setParameter( $categoryIds)
=
=
= -gt
=
-gt
-gt
foreach as
rsquomodelcategory IN (category_ids)rsquo
rsquocategory_idsrsquo
Before Doctrine 25 Doctrine 25+
transform theArrayCollectioninto an array of Ids
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Doctrine
Improved WHERE IN
$categories
$queryBuilder $this
where( )
setParameter( $categories)
=
=
-gt
-gt
rsquomodelcategory IN (categories)rsquo
rsquocategoriesrsquo
Doctrine 25+
WHERE INsupports the useof ArrayCollection
httpdoctrine-ormreadthedocsorgprojectsdoctrine-ormenlatestchangelogmigration_2_5htmlquery-api-where-in-query-using-a-collection-as-parameter
$categories
$categoryIds []
($categories $category)
$categoryIds[] $category getId()
$queryBuilder $this
where( )
setParameter( $categoryIds)
=
=
= -gt
=
-gt
-gt
foreach as
rsquomodelcategory IN (category_ids)rsquo
rsquocategory_idsrsquo
Before Doctrine 25 Doctrine 25+
transform theArrayCollectioninto an array of Ids
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
March
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30 31
1 2 3
27
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Getting the values passed
Twig
Variadic Filters(for unlimited arguments)
httpsgithubcomtwigphptwigpull1699
$filter Twig_SimpleFilter( (
$path $options []
)
[ ])
=
=
=gt
new function
true
rsquoimagersquo
rsquois_variadicrsquo
a single variadic parameter holds anynumber of passed parameters (unlimited)
$filter Twig_SimpleFilter( (
$path $options []
)
$path
$width $options[ ]
$height $options[ ]
$opacity $options[ ]
[ ])
=
=
=
=
=
=
=gt
new function
true
rsquoimagersquo
rsquowidthrsquo
rsquoheight rsquo
rsquoopacity rsquo
rsquois_variadicrsquo
Defining a variadic filter
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
April
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
25 26 27 28 29 3024
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Configuration
Dealing with Sensitive DataAlternative to Environment Variables
appconfigconfigyml
- resource parametersyml
- resource servicesyml
- resource securityyml
- resource adminyml
- resource
ignore_errors true
imports
lsquoetccredentialsexamplecomymlrsquo
1 Create a ldquocredentialsrdquo file in the prod server
3 In ldquodevrdquo machine
2 Load the credentials file in the application
4 In ldquoprodrdquo machine
Credentials file doesnrsquot exist but no error is
triggered (because of ignore_errors)
App uses the regular parametersyml file
Credentials file overrides parametersyml
The right parameters are availableanywhere (eg console commands)
Developers canrsquot see the production configuration options
Requires discipline to addremove options
etccredentialsexamplecomyml
parameters
database_user
database_password
mailer_user
mailer_password
secret
github_client_id
github_client_secret
httpsgithubcomsymfonysymfonypull16403
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
May
sun mon tue wed thu fri sat
6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
1 2 3 4 5
29
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Security
Success and Failure Handlers
httpsgithubcomsymfonysymfony-docsissues4258
Successfailure handlers are available since Symfony 20 (2011) but lots of developers donrsquot use them because they are not documented
The securityinterative_login event
Defining a success handler Enabling the success handler
you just need to implementthis interface
and return a Responseinstance
the most commonevent to ldquodo thingsrdquo after the user successfully logs in
the LoginHandler class isexecuted when the user logs in successfully and after the
eventinteractive_login
services
login_listener
class
arguments
tags
AppBundleListenerLoginListener
]
- name event
[ rsquosecutirytoken_storagersquo lsquodoctrinersquo
lsquokernelevent_listenerrsquo lsquosecurityinteractive_loginrsquo
namespace
use
class implements
public function
return
return new
AppBundleSecurity
SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
LoginHandler AuthenticationSuccessHandlerInterface
onAuthenticationSuccess(Request $request TokenInterface $token)
$this redirect( )
Response( )
do something
-gt rsquorsquo
rsquorsquo
appconfigservicesyml
appconfigsecurityyml
AppBundleSecurityLoginHandler
^
applogin_handler
services
applogin_handler
class
arguments
firewalls
main
pattern
form_login
success_handler
Symfony 20+
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
June
sun mon tue wed thu fri sat
3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
27 28 29 30
1 2
26
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Monolog
Creating Simple Custom Formatters
ignore n inside thelog messages
the format of each log line
the format of
datetime placeholder
Creating custom formatters Enable the custom log formatter
add the formatter option
httpsymfonycomdoccurrentcookbookloggingmonologhtmlchanging-the-formatter
appconfigconfig_devyml
stream
debug
applogformatter
monolog
handlers
main
type
path
level
formatter
ldquokernellogs_dirkernelenvironmentlogrdquo
appconfigconfigyml
false
-
-
- false
services
applogformatter
class
public
arguments
lsquoMonologFormatterLineFormatterrsquo
ldquolevel_name [datetime] [channel] messagen
contextnnrdquo
lsquoHisrsquo
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
July
sun mon tue wed thu fri sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 3031
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Console
Console Style GuideCreate Visually Consistent Commands Effortlessly
All common features are coveredtitle
comment
table
cautionadmonition
section title
listing
all typesof questions
noteadmonition
all types of results
use
class extends
protected function
new
SymfonyComponentConsoleStyleSymfonyStyle
MyCustomCommand ContainerAwareCommand
execute(InputInterface $input OutputInterface $output)
$io SymfonyStyle($input $output)
$io
=
-gt
You can use it in your own commands too
$io = SymfonyStyle($input $output)
$io-gttitle(string $message)
$io-gtsection(string $message)
$io-gttext(string|array $message)
$io-gtcomment(string|array $message)
$io-gtnote(string|array $message)
$io-gtcaution(string|array $message)
$io-gtlisting(array $elements)
$io-gttable(array $headers array $rows)
$io-gtask(string $question string|null $default = null callable|null $validator = null)
$io-gtaskHidden(string $question callable|null $validator = null)
$io-gtconfirm(string $question bool $default = true)
$io-gtchoice(string $question array $choices string|int|null $default = null)
$io-gtsuccess(string|array $message)
$io-gterror(string|array $message)
$io-gtwarning(string|array $message)
new
common output elements
more advanced output elements
ask for users input
display the result of the command or some important task
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
August
sun mon tue wed thu fri sat
5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
29
1 2 3 4
28 30 31
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Form
Field Dependencies
Use POST_ hooks on fields
$builder
add( [ ])
add( [ ])
-gt
-gt
lsquocountryrsquo lsquoentityrsquo
lsquoprovincersquo lsquoentityrsquo
$builder get( ) addEventListener(
FormEventsPOST_SET_DATA
$addProvinceField
)
-gt -gtlsquocountryrsquo
$builder get( ) addEventListener(
FormEventsPOST_SUBMIT
$addProvinceField
)
-gt -gtlsquocountryrsquo
$addProvinceField (FormEvent $event)
$form $event getForm() getParent()
$country $event getData()
$form add( EntityTypeclass [
Provinceclass
($repo) ($country)
])
=
= -gt -gt
= -gt
-gt
=gt
=gt
function
function use
lsquoprovincersquo
lsquoclassrsquo
lsquoquery_builderrsquo
query provinces by $country
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
September
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Composer
Improve Class Loading Performance
New ldquoexclude-from-classmaprdquo optionThese classes are excludedfrom the optimized autoloader
$ composer dump-autoload --optimize
composerjson
[ ]
autoload
exclude-from-classmap Tests test tests
composerjson
[ ]
autoload
exclude-from-classmap Tests testrdquo
matches anything
matches anything except
httpsgithubcomsymfonysymfonypull16397
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
October
sun mon tue wed thu fri sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
25 26 27 28 292330
2431
Symfony first commit(11 years ago)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Tests
Meaningful Data Providers
When an error happens
array keys are the
labels of each data set
public function
return
public function
getUserData()
[
]
testRegistration($register $enable $notify)
lsquoregister user onlyrsquo
lsquoregister and enablersquo
lsquoregister enable and notifyrsquo
dataProvider getUserData
=gt
=gt
=gt
[ ]
[ ]
[ ]
true false false
true true false
true true true
$ phpunit -c app
Time 137 ms Memory 1250Mb
There was 1 failure
1) AppBundleTestsControllerDefaultControllerTesttestRegistration
with (true false false)
F
data set ldquoregister user onlyrdquo
meaninful errormessages
httpwwwthisprogrammingthingcom2015making-dataproviders-more-maintainable
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
November
sun mon tue wed thu fri sat
4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
28 29 30
1 2 3
27
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
Controller
Acessing Request ParametersUseful Methods Defined in the ParameterBag
ParameterBag defines some useful methodsAcessing request parameters
everybody uses
the get() method but there are other useful methods
use
use
use
class extends
public function
SensioBundleFrameworkExtraBundleConfigurationRoute
SymfonyBundleFrameworkBundleControllerController
SymfonyComponentHttpFoundationRequest
BlogController Controller
indexAction()
$value1 $request query get( )
$value2 $request request get( )
Route(rdquoblogrdquo)
Route(rdquordquo name=rdquoblog_indexrdquo)
= -gt -gt
= -gt -gt
rsquoparameter1rsquo
rsquoparameter2rsquo
$pageNumber $request query getInt( )
$isPublished $request query getBoolean( )
$request query getAlpha( )
$request query getAlnum( )
$request query getDigits( )
= -gt -gt
= -gt -gt
-gt -gt
-gt -gt
-gt -gt
rsquopagersquo
rsquopublishedrsquo
rsquoparameterrsquo
rsquoparameterrsquo
rsquoparameterrsquo
lsquoABC+1234DEFrsquo -gt lsquoABCDEFrsquo
lsquoABC+1234DEFrsquo -gt lsquoABC1234DEFrsquo
lsquoABC+1234DEFrsquo -gt lsquo1234rsquo
httpsymfonycomdoccurrentcomponentshttp_foundationintroductionhtml
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
December
sun mon tue wed thu fri sat
2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
26 27 28 29 30
1
25 31
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)
made with by andreiabohner
httpwwwslidesharenetjaviereguiluznew-symfony-tips-tricks-symfonycon-paris-2015 (Thanks javiereguiluz)
httpsspeakerdeckcomwebmozartsymfony2-forms-dos-and-donts (Thanks webmozart)
httpsymfonycomblognew-in-symfony-2-8-console-style-guide
Tips amp Tricks from SymfonyCon 2015 (10 years of Symfony)