Symfony2 - Un Framework PHP 5 Performant

64
Construire des applications web performantes et élégantes avec Symfony2 @hhamon - WebEvent La Ferme du Web – Lyon – 7 mai 2011

description

L’arrivée de PHP 5.3 en milieu d’année 2009 a bouleversé la manière de développer des applications web. En effet, cette nouvelle version a apporté de nouveaux outils au langage tels que les espaces de nommage qui favorisent la réutilisabilité du code lorsqu’ils sont employés à bon escient. Les nouveaux frameworks de développement rapide ont aussi suivi le mouvement et reposent sur la base de PHP 5.3. C’est le cas de Symfony2 dont la version stable est prévue prochainement.Cette présentation offre un tour d’horizon de la nouvelle architecture du framework qui s’articule autour de “bundles”, de librairies externes et de nombreux composants indépendants tels que le conteneur d’injection de dépendances. Nous dresserons un panorama des principales fonctionnalités offertes par le framework telles que la couche d’ORM Doctrine 2, le moteur de templating Twig et la gestion des formulaires. Nous nous intéresserons également à la gestion du cache HTTP, à la couche de sécurité ainsi qu’aux outils de débogage destinés à améliorer la productivité du développeur et la maintenance de l’application.

Transcript of Symfony2 - Un Framework PHP 5 Performant

Page 1: Symfony2 - Un Framework PHP 5 Performant

Construire des applications web performantes et élégantes avec

Symfony2@hhamon - WebEvent La Ferme du Web – Lyon – 7 mai 2011

Page 2: Symfony2 - Un Framework PHP 5 Performant

R&D

Développement

Formation & Coaching

Audit & Conseil

Media

Page 3: Symfony2 - Un Framework PHP 5 Performant
Page 4: Symfony2 - Un Framework PHP 5 Performant

Composants indépendants et

découplés du framework…

Page 5: Symfony2 - Un Framework PHP 5 Performant

Framework « full-stack »

Page 6: Symfony2 - Un Framework PHP 5 Performant

Composants Bibliothèques

Core Bundles + Bridges

Bundles Standards

BundlesMétiers

Bundles Tiers

Configuration

Page 7: Symfony2 - Un Framework PHP 5 Performant

« Un Bundle est un répertoire qui contient une

structure bien précise et qui

héberge tous les fichiers d’une même

fonctionnalité.  »

Page 8: Symfony2 - Un Framework PHP 5 Performant

Un framework simple et unique !

Page 9: Symfony2 - Un Framework PHP 5 Performant

Respectueux des standards

et bonnes pratiques

- RFC2616- PHPUnit

- Jinja Templates- Spring Security- Design Patterns

Page 10: Symfony2 - Un Framework PHP 5 Performant

Simplifier l’installation et la

configuration

http://symfony.com/download

Page 11: Symfony2 - Un Framework PHP 5 Performant

Télécharger l’Edition Standard qui héberge le framework, les bibliothèques tierces et les bundles standards

Distributions disponibles

Page 12: Symfony2 - Un Framework PHP 5 Performant

Configuration simplifiée

Page 13: Symfony2 - Un Framework PHP 5 Performant

Configuration de la base de données.

Page 14: Symfony2 - Un Framework PHP 5 Performant

Démarrage immédiat avec Symfony2.Happy coding

Page 15: Symfony2 - Un Framework PHP 5 Performant

Envie de l’essayer ?

Page 16: Symfony2 - Un Framework PHP 5 Performant

La philosophie de Symfony2

« Convertir une Requête

entrante en une Réponse »

Page 17: Symfony2 - Un Framework PHP 5 Performant

Générer une réponse brute

class DefaultController extends Controller{ /** @Route("/hello/{name}") */ public function indexAction($name) { // ... do things with $name

return new Response('Hello '. $name); }}

Page 18: Symfony2 - Un Framework PHP 5 Performant

Générer un template

class DefaultController extends Controller{ /** @Route("/hello/{name}") */ public function indexAction($name) { // ... do things with $name

return $this->render( 'AcmeHelloBundle:Default:index.html.twig', array('name' => $name) ); }}

Page 19: Symfony2 - Un Framework PHP 5 Performant

Puissance des annotations

class DefaultController extends Controller{ /** * @Route("/schedule") * @Template */ public function indexAction() { $title = 'WebEvent 2011 Schedule';

return array('title' => $title); }}

Page 20: Symfony2 - Un Framework PHP 5 Performant

Templating avec Twig

{% 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 21: Symfony2 - Un Framework PHP 5 Performant

Le moteur de template Twig

Twig est un moteur de templating moderne Rapide

Syntaxe concise et riche Echappement automatique Fonctionnalités modernes Extensible Flexible

Page 22: Symfony2 - Un Framework PHP 5 Performant

Héritage de template

{% 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 %}

index.html.t

wig

Page 23: Symfony2 - Un Framework PHP 5 Performant

Héritage de template

{% extends "::base.html.twig" %}

{% block body %}

<img src="/images/logo.gif" alt="Confoo 2011"/>

{% block content %}{% endblock %}

{% endblock %}

layout.html.t

wig

Page 24: Symfony2 - Un Framework PHP 5 Performant

Héritage de templates

<!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>

base.html.tw

ig

Page 25: Symfony2 - Un Framework PHP 5 Performant

Héritage de template

layout.html.twig

index.html.twig

base.html.twig

Page 26: Symfony2 - Un Framework PHP 5 Performant

Simplifier la définition des URLs

Page 27: Symfony2 - Un Framework PHP 5 Performant

URLs propres

Les URLs classiques

ça craint !!!

Page 28: Symfony2 - Un Framework PHP 5 Performant

URLs propres

Système de routage natif

Page 29: Symfony2 - Un Framework PHP 5 Performant

URL propres

class DefaultController extends Controller{ /** * @Route("/{year}/talk/{month}/{day}/{slug}") * @Template */ public function showAction($slug, $day, $month, $year) { // Get a talk object from the database $talk = ...;

return array('talk' => $talk); }}

Page 30: Symfony2 - Un Framework PHP 5 Performant

Conversion automatique des paramètres

class DefaultController extends Controller{ /** * @Route("/talk/{id}") * @Template */ public function showAction(Talk $talk) { return array('talk' => $talk); }}

Page 31: Symfony2 - Un Framework PHP 5 Performant

Simplifier le Débogage

Page 32: Symfony2 - Un Framework PHP 5 Performant

La barre de débogage

Symfony2 version

PHP environmen

t

Current environment

Current response

Recorded logs

Timers

Memory

Queries

Page 33: Symfony2 - Un Framework PHP 5 Performant

Les traces d’exception

Page 34: Symfony2 - Un Framework PHP 5 Performant

Traces d’exception

Page 35: Symfony2 - Un Framework PHP 5 Performant

Journalisation interne

Page 36: Symfony2 - Un Framework PHP 5 Performant

L’application Profiler

Page 37: Symfony2 - Un Framework PHP 5 Performant

L’application Profiler

Page 38: Symfony2 - Un Framework PHP 5 Performant

Simplifier les interactions avec la BDD

Page 39: Symfony2 - Un Framework PHP 5 Performant

Bibliothèque Doctrine 2

Abstraction de Base de

Données (DBAL)

Mapping Objet Relationnel

(ORM)

Support des Migrations

Mapping Objet Document

(ODM - MongoDB)

Mapping Objet XML (OXM -

XML databases)

Page 40: Symfony2 - Un Framework PHP 5 Performant

Définition des entités Doctrine 2/** @Entity */class Talk{ /** * @Id * @GeneratedValue * @Column(type="integer") */ public $id;

/** @Column(length=80, nullable=false) */ public $title;

/** @Column(type="datetime") */ public $schedule;

/** @ManyToMany(targetEntity="Speaker", mappedBy="talks") */ public $speakers;}

Page 41: Symfony2 - Un Framework PHP 5 Performant

Persistance des entités en BDDclass TalkController extends Controller{ /** @Route("/talks/new") */ public function createAction() { $em = $this->get('registry')->getEntityManager();

$talk = new Talk(); $talk->setTitle('Symfony2, a professional framework'); $talk->setSchedule(new \DateTime('2011-03-12 16:00')); $talk->addSpeaker(new Speaker('Hugo Hamon')); $talk->addSpeaker(new Speaker('Fabien Potencier'));

$em->persist($talk); $em->flush(); }}

Page 42: Symfony2 - Un Framework PHP 5 Performant

Simplifier la validation

des données

Page 43: Symfony2 - Un Framework PHP 5 Performant

Validation

POPOs

Annotations

Extensible

Formulaires

Page 44: Symfony2 - Un Framework PHP 5 Performant

Valider des objets PHP

class ContactRequest{ /** @NotBlank */ public $message;

/** * @Email * @NotBlank */ public $sender;}

Page 45: Symfony2 - Un Framework PHP 5 Performant

Simplifier la gestion des

formulaires

Page 46: Symfony2 - Un Framework PHP 5 Performant

Traitement des formulaires

Valider des objets de Domaine

Protection CSRF Validation Templating avec Twig

Page 47: Symfony2 - Un Framework PHP 5 Performant

class ShopController extends Controller{ public function indexAction() { $product = new Product(); $product->name = 'Test product'; $product->setPrice('50.00');

$form = $this->get('form.factory') ->createBuilder('form', $product) ->add('name', 'text') ->add('price', 'money', array('currency' => 'USD')) ->getForm();

return array('form' => $form->createView()); }}

Page 48: Symfony2 - Un Framework PHP 5 Performant

Traitement du formulairepublic function indexAction(){ $product = new Product(); $form = ...;

$request = $this->get('request'); if ('POST' === $request->getMethod()) {

$form->bindRequest($request);

if ($form->isValid()) { // handle data, persist the object to the database... } }

return array('form' => $form->createView());}

Page 49: Symfony2 - Un Framework PHP 5 Performant

Prototypage avec Twig

<form action="#" method="post">

{{ form_widget(form) }}

<button type="submit"> Create the product

</button>

</form>

Page 50: Symfony2 - Un Framework PHP 5 Performant

Fonctions de rendu avec TwigName Description{{ form_enctype(form) }} Rend un attribut « enctype »

{{ form_errors(form) }}

Rend les erreurs globales du formulaire

{{ form_label(form.age) }}

Rend le label d’un champ « age »

{{ form_errors(form.age) }}

Rend les erreurs d’un champ « age »

{{ form_widget(form.age) }}

Rend le widget d’un champ « age »

{{ form_row(form.age)) }}

Rend une ligne complète d’un champ « age »

{{ form_rest(form) }} Rend les champs non rendus

Page 51: Symfony2 - Un Framework PHP 5 Performant

Simplifier les tests fonctionnels

Page 52: Symfony2 - Un Framework PHP 5 Performant

Tests fonctionnelsSimuler un véritable

navigateur et réaliser des parcours de

navigation sur l’application

Page 53: Symfony2 - Un Framework PHP 5 Performant

Tests Fonctionnels

class DefaultControllerTest extends WebTestCase{ public function testIndex() { $client = $this->createClient(); $crawler = $client->request('GET', '/schedule');

$this->assertTrue( $crawler->filter('html:contains("Fabien")')->count() > 0 );

$response = $client->getResponse(); $this->assertTrue($response->headers->has('expires')); }}

Page 54: Symfony2 - Un Framework PHP 5 Performant

Simplifier le cache des pages

Page 55: Symfony2 - Un Framework PHP 5 Performant

Expiration & Validation

Page 56: Symfony2 - Un Framework PHP 5 Performant

Expiration avec Expires

class DefaultController extends Controller{ /** * @Route("/schedule") * @Template * @Cache(expires="tomorrow") */ public function indexAction() { $title = 'Confoo 2011 Conferences Schedule';

return array('title' => $title); }}

Page 57: Symfony2 - Un Framework PHP 5 Performant

Expiration avec Cache-Control

class DefaultController extends Controller{ /** * @Route("/schedule") * @Template * @Cache(maxage="20") */ public function indexAction() { $title = 'Confoo 2011 Conferences Schedule';

return array('title' => $title); }}

Page 58: Symfony2 - Un Framework PHP 5 Performant

PHPVarnishSquid

Page 59: Symfony2 - Un Framework PHP 5 Performant

http://www.varnish-cache.org

Page 60: Symfony2 - Un Framework PHP 5 Performant

Edge Side Includes

<esi:include src="http://..." />

Page 61: Symfony2 - Un Framework PHP 5 Performant
Page 62: Symfony2 - Un Framework PHP 5 Performant
Page 63: Symfony2 - Un Framework PHP 5 Performant

Authentification

Authorization

Page 64: Symfony2 - Un Framework PHP 5 Performant

Merci !

Sensio Labs recrute !