symfony - introduction
TRANSCRIPT
A Better Way to Build
Thursday, 27 September 12
Piers Warmers
@warmers
@pierswarmers
Thursday, 27 September 12
What’s on tonight?
Thursday, 27 September 12
Session 1.
Thursday, 27 September 12
Session 1.
• Introduction
Thursday, 27 September 12
Session 1.
• Introduction
• What is Symfony?
Thursday, 27 September 12
Session 1.
• Introduction
• What is Symfony?
• Core concepts
Thursday, 27 September 12
Session 1.
• Introduction
• What is Symfony?
• Core concepts
• A typical application
Thursday, 27 September 12
Session 1.
• Introduction
• What is Symfony?
• Core concepts
• A typical application
• Lets code!
Thursday, 27 September 12
Session 2.
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
• Cleaning up our interfaces
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
• An overview of security
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
• An overview of security
• Using Symfony components without symfony
Thursday, 27 September 12
Session 2.
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
• An overview of security
• Using Symfony components without symfony
Thursday, 27 September 12
ce qui est Symfony?
Thursday, 27 September 12
Symfony is…
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+)
Thursday, 27 September 12
“Symfony is a reusable set of standalone, decoupled and cohesive PHP components that solve common
web development problems”Dries Buytaert (Founder of Drupal)
Thursday, 27 September 12
Problems?
Thursday, 27 September 12
Problems?
• Connect requests to business logic (route ➜ code)
Thursday, 27 September 12
Problems?
• Connect requests to business logic (route ➜ code)
• Handle environment configuration (dev, test, prod)
Thursday, 27 September 12
Problems?
• Connect requests to business logic (route ➜ code)
• Handle environment configuration (dev, test, prod)
• Secure an application (authentication, firewalls)
Thursday, 27 September 12
Problems?
• Connect requests to business logic (route ➜ code)
• Handle environment configuration (dev, test, prod)
• Secure an application (authentication, firewalls)
• Render a response (templates)
Thursday, 27 September 12
Problems?
• Connect requests to business logic (route ➜ code)
• Handle environment configuration (dev, test, prod)
• Secure an application (authentication, firewalls)
• Render a response (templates)
• Speed things up (compiling, caching, ESI, proxies)
Thursday, 27 September 12
Problems?
• Connect requests to business logic (route ➜ code)
• Handle environment configuration (dev, test, prod)
• Secure an application (authentication, firewalls)
• Render a response (templates)
• Speed things up (compiling, caching, ESI, proxies)
• I18N & L10N (translations, regional formatting)
Thursday, 27 September 12
Standalone, decoupled?
Thursday, 27 September 12
Symfony & Components
BrowserKit
ClassLoader
Config
Console
CssSelector
DependencyInjection
DomCrawler
EventDispatcher
Finder
Form
HttpFoundation
HttpKernel
Locale
Process
Routing
Security
Serializer
Templating
Translation
Validator
Yaml
includes
See: http://symfony.com/components
Thursday, 27 September 12
“Let's try to eliminate the NIH
syndrome in the PHP world!”Fabien Potencier (Founder of Symfony)
Thursday, 27 September 12
Drupal & Symfony
BrowserKit
ClassLoader
Config
Console
CssSelector
DependencyInjection
DomCrawler
EventDispatcher
Finder
Form
HttpFoundation
HttpKernel
Locale
Process
Routing
Security
Serializer
Templating
Translation
Validator
Yaml
using
Version 8+
Thursday, 27 September 12
https://github.com/drupal/drupal/blob/8.x/index.php
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+) ✓
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+) ✓
• application methodology (best practices, standards)
Thursday, 27 September 12
“A most important, but also most elusive, aspect of any tool is its
influence on the habits of those who
train themselves in its use.”Edsger Dijkstra (Computer Scientist)
Thursday, 27 September 12
Influence by example…
Thursday, 27 September 12
Influence by example…
• a blueprint for applications (shape, vernacular)
Thursday, 27 September 12
Influence by example…
• a blueprint for applications (shape, vernacular)
• best practices (quality, testing)
Thursday, 27 September 12
Influence by example…
• a blueprint for applications (shape, vernacular)
• best practices (quality, testing)
• standardisation and consistency (PSR-0, PSR-1, PSR-2)
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+) ✓
• application methodology (best practices, standards) ✓
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+) ✓
• application methodology (best practices, standards) ✓
• an enthusiastic community (it’s about people)
Thursday, 27 September 12
…and theres lots of them!
Thursday, 27 September 12
…and theres lots of them!
• large community (http://connect.sensiolabs.com)
Thursday, 27 September 12
…and theres lots of them!
• large community (http://connect.sensiolabs.com)
• regional events (SymfonyLive - Paris, London, Berlin, San Francisco)
Thursday, 27 September 12
…and theres lots of them!
• large community (http://connect.sensiolabs.com)
• regional events (SymfonyLive - Paris, London, Berlin, San Francisco)
• involved (http://symfony.com/contributors)
Thursday, 27 September 12
…and theres lots of them!
• large community (http://connect.sensiolabs.com)
• regional events (SymfonyLive - Paris, London, Berlin, San Francisco)
• involved (http://symfony.com/contributors)
• like to share (open source, permissive MIT style)
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+) ✓
• application methodology (best practices, standards) ✓
Thursday, 27 September 12
Symfony is…
• a full stack framework (PHP 5.3+) ✓
• application methodology (best practices, standards) ✓
• an enthusiastic community (it’s about people) ✓
Thursday, 27 September 12
Lets Go!Time to setup a project.
Thursday, 27 September 12
Installation in three steps
Thursday, 27 September 12
Installation in three steps
• get composer
Thursday, 27 September 12
Installation in three steps
• get composer
• create project
Thursday, 27 September 12
Installation in three steps
• get composer
• create project
• start server
Thursday, 27 September 12
Dependency management
Satis
Thursday, 27 September 12
So the installation in three steps…
Thursday, 27 September 12
So the installation in three steps…
Thursday, 27 September 12
Get composer - it’s crazy simple!
Thursday, 27 September 12
Get composer - it’s crazy simple!
Thursday, 27 September 12
Get composer - it’s crazy simple!
Thursday, 27 September 12
Get composer - it’s crazy simple!
Thursday, 27 September 12
Create a project
Thursday, 27 September 12
Create a project
Thursday, 27 September 12
Create a project
Thursday, 27 September 12
Create a project
Thursday, 27 September 12
Run the server, we’re online!
Thursday, 27 September 12
Lets look inside ./demo1
Thursday, 27 September 12
Lets look inside ./demo1
Thursday, 27 September 12
The important parts…
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
• composer.json ➜ dependancy map
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
• composer.json ➜ dependancy map
• composer.lock ➜ what was actually installed
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
• composer.json ➜ dependancy map
• composer.lock ➜ what was actually installed
• src ➜ you’re code goes here
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
• composer.json ➜ dependancy map
• composer.lock ➜ what was actually installed
• src ➜ you’re code goes here
• vendor ➜ other peoples code goes here (via composer)
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
• composer.json ➜ dependancy map
• composer.lock ➜ what was actually installed
• src ➜ you’re code goes here
• vendor ➜ other peoples code goes here (via composer)
• web ➜ server loads app.php here which loads…
Thursday, 27 September 12
The important parts…
• app ➜ AppKernal, cache, configuration, logs, console
• composer.json ➜ dependancy map
• composer.lock ➜ what was actually installed
• src ➜ you’re code goes here
• vendor ➜ other peoples code goes here (via composer)
• web ➜ server loads app.php here which loads…
AppKernal.php
Thursday, 27 September 12
In the browser
Thursday, 27 September 12
The console
Thursday, 27 September 12
./app/console
Thursday, 27 September 12
The console
Thursday, 27 September 12
The console
• Time savers: build models, controllers and bundles
Thursday, 27 September 12
The console
• Time savers: build models, controllers and bundles
• Manage cache removal
Thursday, 27 September 12
The console
• Time savers: build models, controllers and bundles
• Manage cache removal
• Update database schema
Thursday, 27 September 12
The console
• Time savers: build models, controllers and bundles
• Manage cache removal
• Update database schema
• Send spooled emails
Thursday, 27 September 12
The console
• Time savers: build models, controllers and bundles
• Manage cache removal
• Update database schema
• Send spooled emails
• Build you own CLI tasks
Thursday, 27 September 12
The console
• Time savers: build models, controllers and bundles
• Manage cache removal
• Update database schema
• Send spooled emails
• Build you own CLI tasks
• Run the PHP test server
Thursday, 27 September 12
Built in PHP server is good for quick testing but... don’t use in production!
Thursday, 27 September 12
What’s my normal setup?
Thursday, 27 September 12
What’s my normal setup?
• PHP 5.4.6
Thursday, 27 September 12
What’s my normal setup?
• PHP 5.4.6
• MySQL 5.5.25a MySQL Community Server
Thursday, 27 September 12
What’s my normal setup?
• PHP 5.4.6
• MySQL 5.5.25a MySQL Community Server
• Apache HTTP Server 2.2.22
Thursday, 27 September 12
Let’s look at the config…
Thursday, 27 September 12
Configure: hosts
Thursday, 27 September 12
Configure: Apache HTTP
Thursday, 27 September 12
Configure: MySQL CE
Thursday, 27 September 12
Through Apache
Thursday, 27 September 12
The Web Debug Toolbar
Thursday, 27 September 12
Web Debug Toolbar
Thursday, 27 September 12
Web Debug Toolbar
• Displays resource usage
Thursday, 27 September 12
Web Debug Toolbar
• Displays resource usage
• Monitors code execution times
Thursday, 27 September 12
Web Debug Toolbar
• Displays resource usage
• Monitors code execution times
• Presents info on session, logs, exceptions
Thursday, 27 September 12
Web Debug Toolbar
• Displays resource usage
• Monitors code execution times
• Presents info on session, logs, exceptions
• Provides insight into persistence layer
Thursday, 27 September 12
Web Debug Toolbar
• Displays resource usage
• Monitors code execution times
• Presents info on session, logs, exceptions
• Provides insight into persistence layer
• Good for showing off in meetings
Thursday, 27 September 12
“Hello world!”The Demo App
Thursday, 27 September 12
What’s just happened?
Route
htpp://…/hello/SydPHP
Request
Controller
View
Thursday, 27 September 12
What’s just happened?
/demo/hello/{name}
htpp://…/demo/hello/SydPHP
authentication, authorisation, resolving etc…
AcmeDemoBundle->helloAction($name)
hello.html.twig
layout.html.twig
Thursday, 27 September 12
./app/config/routing_dev.yml
Thursday, 27 September 12
./src/Acme/DemoBundle/Controller/DemoController.php
Thursday, 27 September 12
./src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig
Thursday, 27 September 12
A template enginefor PHP?
Thursday, 27 September 12
Twig template engine
Thursday, 27 September 12
Twig template engine
• Fast (compiled to optimised PHP)
Thursday, 27 September 12
Twig template engine
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
Thursday, 27 September 12
For example:
Output in PHP:
Output in Twig:
Thursday, 27 September 12
For example:
Output in PHP:
Output in Twig:
Thursday, 27 September 12
Twig templating
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
Thursday, 27 September 12
Twig templating
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
• Secure (escaped output, sandboxed)
Thursday, 27 September 12
Twig templating
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
• Secure (escaped output, sandboxed)
• Extendable (custom tags and filters)
Thursday, 27 September 12
Twig templating
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
• Secure (escaped output, sandboxed)
• Extendable (custom tags and filters)
• Helps enforce SoC (helps keep templates for display only)
Thursday, 27 September 12
Twig templating
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
• Secure (escaped output, sandboxed)
• Extendable (custom tags and filters)
• Helps enforce SoC (helps keep templates for display only)
• Totally optional (you can still use PHP or both)
Thursday, 27 September 12
Twig templating
• Fast (compiled to optimised PHP)
• Concise (easy to learn, clean, designers will love you)
• Secure (escaped output, sandboxed)
• Extendable (custom tags and filters)
• Helps enforce SoC (helps keep templates for display only)
• Totally optional (you can still use PHP or both)
Thursday, 27 September 12
Twig templates can extend Layouts
Thursday, 27 September 12
(simplified)./src/Acme/DemoBundle/Resources/views/layout.html.twig
Thursday, 27 September 12
Coming up…
Thursday, 27 September 12
Coming up…
• CRUD and MVC (with the model)
Thursday, 27 September 12
Coming up…
• CRUD and MVC (with the model)
• Cleaning up our interfaces
Thursday, 27 September 12
Coming up…
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
Thursday, 27 September 12
Coming up…
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
Thursday, 27 September 12
Coming up…
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
• An overview of security
Thursday, 27 September 12
Coming up…
• CRUD and MVC (with the model)
• Cleaning up our interfaces
• Forms and validation
• Services and dependancy injection
• An overview of security
• Using Symfony components without symfony
Thursday, 27 September 12
big thanks to
SensioLabs
PizzaTime!
Thursday, 27 September 12
Quick recap…
Thursday, 27 September 12
Quick recap…
• We have a simple application setup
Thursday, 27 September 12
Quick recap…
• We have a simple application setup
• We’ve looked at:
Thursday, 27 September 12
Quick recap…
• We have a simple application setup
• We’ve looked at:
• routing
Thursday, 27 September 12
Quick recap…
• We have a simple application setup
• We’ve looked at:
• routing
• controllers
Thursday, 27 September 12
Quick recap…
• We have a simple application setup
• We’ve looked at:
• routing
• controllers
• templates
Thursday, 27 September 12
Quick recap…
• We have a simple application setup
• We’ve looked at:
• routing
• controllers
• templates
• Lets build a simple CRUD application
Thursday, 27 September 12
CRUD and MVC
Route
htpp://…/products
Request
Controller
Model View
Thursday, 27 September 12
Doctrine DBAL and ORM
Thursday, 27 September 12
Before we get started…
Thursday, 27 September 12
Bundles & namespacesare your friends…
Thursday, 27 September 12
Bundles & namespacesare your friends…
image by sergiu_bacioiu @
Thursday, 27 September 12
• Make life simpler (after some wtf moments)
Thursday, 27 September 12
• Make life simpler (after some wtf moments)
• Should follow FIG standards (www.php-fig.org)
Thursday, 27 September 12
• Make life simpler (after some wtf moments)
• Should follow FIG standards (www.php-fig.org)
• Makes autoloading a breeze (ClassLoader component)
See here: http://symfony.com/doc/current/components/class_loader.html
Thursday, 27 September 12
• \Zend\Mail\Message ➜
/path/to/project/lib/vendor/Zend/Mail/Message.php
Thursday, 27 September 12
• \Zend\Mail\Message ➜
/path/to/project/lib/vendor/Zend/Mail/Message.php
• \Symfony\Core\Request ➜
/path/to/project/lib/vendor/Symfony/Core/Request.php
Thursday, 27 September 12
Lets create our own bundleAcme/Bundle/BlogBundle
Thursday, 27 September 12
./app/config/parameters.ymlGenerate the new bundle
Thursday, 27 September 12
./app/config/parameters.ymlGenerate the new bundle
Thursday, 27 September 12
The modelLets create our Doctrine entity
Thursday, 27 September 12
./app/config/parameters.ymlGenerate the new entity
Thursday, 27 September 12
The CRUD logicLets create our Controller
Thursday, 27 September 12
./app/config/parameters.ymlGenerate the new entity
Thursday, 27 September 12
2 Errors
Thursday, 27 September 12
Oh snap: part 1
Thursday, 27 September 12
./app/config/parameters.yml will fix this
Thursday, 27 September 12
Oh snap: part 2
Thursday, 27 September 12
Update the databaseThere’s a task for that.
Thursday, 27 September 12
./app/config/parameters.ymlGenerate the update query
Thursday, 27 September 12
Looks good, sort of?
Thursday, 27 September 12
Cleaning up
Thursday, 27 September 12
Cleaning up
• Add our new layout template (apply it to views)
Thursday, 27 September 12
Cleaning up
• Add our new layout template (apply it to views)
• Add some CSS (quick look at asset linking)
Thursday, 27 September 12
Cleaning up
• Add our new layout template (apply it to views)
• Add some CSS (quick look at asset linking)
• Discover Assetic (images, css, javascript etc…)
Thursday, 27 September 12
./app/config/parameters.ymlLets use this simple template
Thursday, 27 September 12
./app/config/parameters.ymlAnd apply it to our templates
Thursday, 27 September 12
Why this?{{ asset('bundles/acmeblog/css/demo.css') }}
Thursday, 27 September 12
Why this?{{ asset('bundles/acmeblog/css/demo.css') }}
• Can prefix different paths (dev vs prod)
Thursday, 27 September 12
Why this?{{ asset('bundles/acmeblog/css/demo.css') }}
• Can prefix different paths (dev vs prod)
• Can be used for cache busting (v1, v2, v3, etc…)
Thursday, 27 September 12
./app/config/parameters.yml./app/config/config.yml
Thursday, 27 September 12
Assetic is the next step
Thursday, 27 September 12
Assetic is the next step
• Advanced filters:
Thursday, 27 September 12
Assetic is the next step
• Advanced filters:
• Images (jpegoptim, optipng)
Thursday, 27 September 12
Assetic is the next step
• Advanced filters:
• Images (jpegoptim, optipng)
• CSS (combine and compress)
Thursday, 27 September 12
Assetic is the next step
• Advanced filters:
• Images (jpegoptim, optipng)
• CSS (combine and compress)
• JS (combine and compress)
Thursday, 27 September 12
Assetic is the next step
• Advanced filters:
• Images (jpegoptim, optipng)
• CSS (combine and compress)
• JS (combine and compress)
• LESS, SASS (compile, combine, compress)
Thursday, 27 September 12
./app/config/parameters.ymlUpdate the layout…
Thursday, 27 September 12
Forms & Validation
Thursday, 27 September 12
Forms Widgets
Thursday, 27 September 12
./app/config/parameters.ymlSimple, elegant presentation
Thursday, 27 September 12
Constraints & Assertions
Thursday, 27 September 12
./app/config/parameters.ymlAssertions in annotations
Thursday, 27 September 12
Services
Thursday, 27 September 12
Lets improve Postwho’s setting the “created_at” property?
Thursday, 27 September 12
./app/config/parameters.ymlIn the model?
Thursday, 27 September 12
./app/config/parameters.ymlOn the object?
Thursday, 27 September 12
Maybe there’s a better way?
Thursday, 27 September 12
./app/config/parameters.ymlServices are global toolkits
Thursday, 27 September 12
Service ContainersDependancy Injection
Thursday, 27 September 12
“Dependency injection is a software design pattern that allows a choice of component to be made at run-time rather than
compile time. This can be used, for example, as a simple way to load plugins dynamically or to choose mock objects in test
environments vs. real objects in production environments.”Wikipedia
Thursday, 27 September 12
Finishing up
Thursday, 27 September 12
Finishing up
• Security (broad topic, demo app has an example)
Thursday, 27 September 12
Finishing up
• Security (broad topic, demo app has an example)
• Firewalls (protect areas of application via routes)
Thursday, 27 September 12
Finishing up
• Security (broad topic, demo app has an example)
• Firewalls (protect areas of application via routes)
• Authentication providers (in memory, HTTP, build you own, FOSUserBundle)
Thursday, 27 September 12
Finishing up
• Security (broad topic, demo app has an example)
• Firewalls (protect areas of application via routes)
• Authentication providers (in memory, HTTP, build you own, FOSUserBundle)
• Authorisation (ACL in config or DB)
Thursday, 27 September 12
Out of time…Thursday, 27 September 12
A huge thanks to…
• SensioLabs: t-shirts, pizza, support
• SydPHP: organisation, support
Thursday, 27 September 12
Slides will be up on Speaker Deck shortly…
@warmers
Thursday, 27 September 12