build powerfull and smart web applications with symfony2
DESCRIPTION
Symfony2 first stable release is scheduled for the first week of March 2011. During this session, we will have a look at the new framework architecture and most of its powerfull features. We will show you how Symfony2 implements the MVC pattern and an HTTP request is processed and converted as a response for the end user. Of course, we will talk about the configuration principles and how it's easy to configure Symfony2 project parts like the routing system or the data model. We will also focus on other major components of the framework like the Doctrine2 integration, forms, security (authentication and authorizations) or HTTP cache management.TRANSCRIPT
![Page 1: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/1.jpg)
Build smart and powerful web applications with Symfony2
![Page 2: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/2.jpg)
![Page 3: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/3.jpg)
Built around standalone and decoupled components…
![Page 4: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/4.jpg)
… and a full-stack framework based on those components
![Page 5: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/5.jpg)
Application bundles Third party bundles
Standalone Components
Core Bundles Third party libraries
The Symfony2 stack
![Page 6: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/6.jpg)
« A Bundle is a directory that has a well-de"ned structure and can host anything from classes to
controllers and web resources. »
![Page 7: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/7.jpg)
What makes Symfony2 unique?
![Page 8: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/8.jpg)
Symfony2 follows standards & best practices
- RFC2616 - PHPUnit
- Jinja Templates - Design Patterns
![Page 9: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/9.jpg)
Symfony is now easier to install and con"gure
http://symfony.com/download
![Page 10: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/10.jpg)
Download the Standard Edition that hosts the framework, standard bundles and a default application architecture.
Several distributions available
![Page 11: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/11.jpg)
Easy installation and con!guration
![Page 12: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/12.jpg)
Web con!guration Con"gure the database access parameters
![Page 13: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/13.jpg)
Start to use Symfony2 and happy coding J
![Page 14: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/14.jpg)
Want to give it a try?
![Page 15: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/15.jpg)
Symfony2 Philosophy
« Basically, Symfony2 asks you to convert a Request into a Response »
![Page 16: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/16.jpg)
Request handling
class DefaultController extends Controller { /** * @extra:Route("/hello/{name}") */ public function indexAction($name) { // ... do things return new Response(sprintf('Hello %s!', $name)); } }
![Page 17: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/17.jpg)
Request handling
class DefaultController extends Controller { /** * @extra:Route("/hello/{name}") */ public function indexAction($name) { // ... do things return $this->render('HelloBundle:Default:index.html.twig', array('name' => $name)); } }
![Page 18: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/18.jpg)
Request handling
class DefaultController extends Controller { /** * @extra:Route("/schedule") * @extra:Template */ public function indexAction() { $title = 'Confoo 2011 Conferences Schedule'; return array('title' => $title); } }
![Page 19: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/19.jpg)
Templating
{% extends "ConfooConferenceBundle::layout.html.twig" %} {% block content %} <h1> {{ title }} </h1> <ul> <li>Caching on the Edge, by Fabien Potencier</li> <li>HipHop for PHP, by Scott Mac Vicar</li> <li>XDebug, by Derick Rethans</li> <li>...</li> </ul> {% endblock %}
![Page 20: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/20.jpg)
TWIG Template Engine
Twig is a modern template engine for PHP
§ Fast § Concise and rich syntax § Automatic output escaping § Modern features § Extensible § Flexible
![Page 21: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/21.jpg)
Template inheritance
{% extends "ConfooConferenceBundle::layout.html.twig" %} {% block content %} <h1> {{ title }} </h1> <ul> <li>Caching on the Edge, by Fabien Potencier</li> <li>HipHop for PHP, by Scott Mac Vicar</li> <li>XDebug, by Derick Rethans</li> <li>...</li> </ul> {% endblock %}
![Page 22: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/22.jpg)
Template inheritance
{% extends "::base.html.twig" %} {% block body %} <img src="/images/logo.gif" alt="Confoo 2011"/> {% block content %}{% endblock %} {% endblock %}
![Page 23: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/23.jpg)
Template inheritance
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>{% block title %}Welcome!{% endblock %}</title> <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" /> </head> <body> {% block body %}{% endblock %} </body> </html>
![Page 24: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/24.jpg)
Template inheritance
layout.html.twig
index.html.twig
base.html.twig
![Page 25: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/25.jpg)
Smart URIs
![Page 26: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/26.jpg)
Smart URIs
Typical PHP URIs suck!!!
![Page 27: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/27.jpg)
Smart URIs
Native routing mechanism
![Page 28: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/28.jpg)
Smart URIs
class DefaultController extends Controller { /** * @extra:Route("/{year}/talk/{month}/{day}/{slug}") * @extra:Template */ public function showAction($slug, $day, $month, $year) { // Get a talk object from the database $talk = ...; return array('talk' => $talk); } }
![Page 29: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/29.jpg)
Parameter converter
class DefaultController extends Controller { /** * @extra:Route("/talk/{id}") * @extra:Template */ public function showAction(Talk $talk) { return array('talk' => $talk); } }
![Page 30: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/30.jpg)
Easy Debugging
![Page 31: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/31.jpg)
The Web Debug Toolbar
Symfony2 version PHP environment Current environment Current response Recorded logs Timers Memory Queries
![Page 32: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/32.jpg)
Exception stack traces
![Page 33: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/33.jpg)
Exception stack traces
![Page 34: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/34.jpg)
Recorded logs
![Page 35: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/35.jpg)
The Pro!ler application
![Page 36: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/36.jpg)
The Pro!ler application
![Page 37: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/37.jpg)
Database Management
![Page 38: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/38.jpg)
Doctrine 2 Library
§ Database Abstraction Layer on top of PDO
§ Object Relational Mapper
§ Migrations support
§ Object Document Mapper (MongoDB)
§ Object XML Mapper ( XML databases)
![Page 39: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/39.jpg)
De!ning entities as POPO /** * @orm:Entity */class Talk{ /** * @orm:Id * @orm:GeneratedValue * @orm:Column(type="integer") */ public $id; /** @orm:Column(length=80, nullable=false) */ public $title; /** @orm:Column(type="text") */ public $synopsis; /** @orm:Column(type="datetime") */ public $schedule; /** @orm:ManyToMany(targetEntity="Speaker", mappedBy="talks") */ public $speakers;}
![Page 40: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/40.jpg)
Validation
![Page 41: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/41.jpg)
Validation
§ Validate POPOs (properties & methods)
§ Easy con"guration with annotations
§ Easy to customize and extend
![Page 42: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/42.jpg)
Validating Plain PHP Objects
class ContactRequest { /** @validation:NotBlank */ public $message; /** * @validation:Email * @validation:NotBlank */ public $sender; } }
![Page 43: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/43.jpg)
Forms Handling
![Page 44: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/44.jpg)
Forms management
§ Transparent layer on top of your domain object
§ Native CSRF protection
§ Coupled to the Validation framework
§ Twig integration
![Page 45: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/45.jpg)
Designing a basic form class
namespace Confoo\ContactBundle\Form; use Symfony\Component\Form\Form; use Symfony\Component\Form\TextField; use Symfony\Component\Form\TextareaField; use Symfony\Component\Form\CheckboxField; class ContactForm extends Form { protected function configure() { $this->add(new TextField('sender'))); $this->add(new TextareaField('message')); } }
![Page 46: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/46.jpg)
Processing a form
public function contactAction() { $contactRequest = new ContactRequest(); $form = ContactForm::create(...); $form->bind($this->get('request'), $contactRequest); if ($form->isValid()) { // do things with validated data } return array('form' => $form); }
![Page 47: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/47.jpg)
Prototyping the rendering with Twig
{% extends 'ConfooContactBundle::layout.html.twig' %} {% block content %} <form action="#" method="post"> {{ form_field(form) }} <input type="submit" value="Send!" /> </form> {% endblock %}
![Page 48: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/48.jpg)
Functional Testing
![Page 49: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/49.jpg)
Functional testing
Simulating an end-user browsing scenario and testing the Response
![Page 50: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/50.jpg)
Functional Testing
class DefaultControllerTest extends WebTestCase { public function testIndex() { $client = $this->createClient(); $crawler = $client->request('GET', '/schedule'); $this->assertTrue( $crawler->filter('html:contains("Fabien Potencier")')->count() > 0 ); $this->assertTrue($client->getResponse()->headers->has('expires')); } }
![Page 51: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/51.jpg)
HTTP Compliance (RFC2616)
![Page 52: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/52.jpg)
Expiration / Validation
![Page 53: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/53.jpg)
Expiration with Expires
class DefaultController extends Controller { /** * @extra:Route("/schedule") * @extra:Template * @extra:Cache(expires="tomorrow") */ public function indexAction() { $title = 'Confoo 2011 Conferences Schedule'; return array('title' => $title); } }
![Page 54: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/54.jpg)
Expiration with Cache-Control
class DefaultController extends Controller { /** * @extra:Route("/schedule") * @extra:Template * @extra:Cache(maxage="20", s-maxage="20") */ public function indexAction() { $title = 'Confoo 2011 Conferences Schedule'; return array('title' => $title); } }
![Page 55: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/55.jpg)
Native PHP Reverse Proxy Cache
![Page 56: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/56.jpg)
Varnish / Squid
![Page 57: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/57.jpg)
Edge Side Includes
<esi:include src="http://..." />
![Page 58: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/58.jpg)
Security Authentication & Authorization
![Page 59: Build powerfull and smart web applications with Symfony2](https://reader034.vdocuments.site/reader034/viewer/2022052307/554f78e1b4c905d25b8b4661/html5/thumbnails/59.jpg)
Thank You!