Symfonising with Drupal 8: Symfony2, Drupal 8, PHP-FIG and Why it Matters
Symfony2 - Un Framework PHP 5 Performant
-
Upload
hugo-hamon -
Category
Technology
-
view
9.068 -
download
3
description
Transcript of Symfony2 - Un Framework PHP 5 Performant
![Page 1: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/2.jpg)
R&D
Développement
Formation & Coaching
Audit & Conseil
Media
![Page 3: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/3.jpg)
![Page 4: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/4.jpg)
Composants indépendants et
découplés du framework…
![Page 5: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/5.jpg)
Framework « full-stack »
![Page 6: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/6.jpg)
Composants Bibliothèques
Core Bundles + Bridges
Bundles Standards
BundlesMétiers
Bundles Tiers
Configuration
![Page 7: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/7.jpg)
« 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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/8.jpg)
Un framework simple et unique !
![Page 9: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/9.jpg)
Respectueux des standards
et bonnes pratiques
- RFC2616- PHPUnit
- Jinja Templates- Spring Security- Design Patterns
![Page 10: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/10.jpg)
Simplifier l’installation et la
configuration
http://symfony.com/download
![Page 11: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/12.jpg)
Configuration simplifiée
![Page 13: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/13.jpg)
Configuration de la base de données.
![Page 14: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/14.jpg)
Démarrage immédiat avec Symfony2.Happy coding
![Page 15: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/15.jpg)
Envie de l’essayer ?
![Page 16: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/16.jpg)
La philosophie de Symfony2
« Convertir une Requête
entrante en une Réponse »
![Page 17: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/25.jpg)
Héritage de template
layout.html.twig
index.html.twig
base.html.twig
![Page 26: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/26.jpg)
Simplifier la définition des URLs
![Page 27: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/27.jpg)
URLs propres
Les URLs classiques
ça craint !!!
![Page 28: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/28.jpg)
URLs propres
Système de routage natif
![Page 29: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/31.jpg)
Simplifier le Débogage
![Page 32: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/32.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/33.jpg)
Les traces d’exception
![Page 34: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/34.jpg)
Traces d’exception
![Page 35: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/35.jpg)
Journalisation interne
![Page 36: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/36.jpg)
L’application Profiler
![Page 37: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/37.jpg)
L’application Profiler
![Page 38: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/38.jpg)
Simplifier les interactions avec la BDD
![Page 39: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/39.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/40.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/41.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/42.jpg)
Simplifier la validation
des données
![Page 43: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/43.jpg)
Validation
POPOs
Annotations
Extensible
Formulaires
![Page 44: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/44.jpg)
Valider des objets PHP
class ContactRequest{ /** @NotBlank */ public $message;
/** * @Email * @NotBlank */ public $sender;}
![Page 45: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/45.jpg)
Simplifier la gestion des
formulaires
![Page 46: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/46.jpg)
Traitement des formulaires
Valider des objets de Domaine
Protection CSRF Validation Templating avec Twig
![Page 47: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/47.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/48.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/49.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/50.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/51.jpg)
Simplifier les tests fonctionnels
![Page 52: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/52.jpg)
Tests fonctionnelsSimuler un véritable
navigateur et réaliser des parcours de
navigation sur l’application
![Page 53: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/53.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/54.jpg)
Simplifier le cache des pages
![Page 55: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/55.jpg)
Expiration & Validation
![Page 56: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/56.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/57.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/58.jpg)
PHPVarnishSquid
![Page 59: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/59.jpg)
http://www.varnish-cache.org
![Page 60: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/60.jpg)
Edge Side Includes
<esi:include src="http://..." />
![Page 61: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/61.jpg)
![Page 62: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/62.jpg)
![Page 63: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/63.jpg)
Authentification
Authorization
![Page 64: Symfony2 - Un Framework PHP 5 Performant](https://reader035.fdocuments.net/reader035/viewer/2022062307/554fb98eb4c9050e7d8b47b0/html5/thumbnails/64.jpg)
Merci !
Sensio Labs recrute !