Speed up your developments with Symfony2

71
Speed Up Your Developments with Symfony2

description

 

Transcript of Speed up your developments with Symfony2

Page 1: Speed up your developments with Symfony2

Speed Up Your Developments with

Symfony2

Page 2: Speed up your developments with Symfony2
Page 3: Speed up your developments with Symfony2

Hugo HamonTrainings Manager

hhamon on TwitterInternational speaker

Page 4: Speed up your developments with Symfony2

Built around standalone and

decoupled components…

Page 5: Speed up your developments with Symfony2

… and a full-stack framework based on those components

Page 6: Speed up your developments with Symfony2

Application bundles

Third party bundles

Standalone Components

Core Bundles

Third party libraries

The Symfony2 stack

Bridges

Page 7: Speed up your developments with Symfony2

« A Bundle is a directory that has a well-defined structure and can host

anything from classes to controllers and web

resources.  »

Page 8: Speed up your developments with Symfony2

What makes Symfony2 unique?

Page 9: Speed up your developments with Symfony2

Symfony2 follows standards

& best practices

- RFC2616- PHPUnit

- Jinja Templates- Design Patterns

Page 10: Speed up your developments with Symfony2

Symfony is now easier to install and configure

http://symfony.com/download

Page 11: Speed up your developments with Symfony2

Download the Standard Edition that hosts the framework, standard bundles and a default application architecture.

Several distributions available

Page 12: Speed up your developments with Symfony2

Easy installation and configuration

Page 13: Speed up your developments with Symfony2

Web configuration

Configure the database access parameters

Page 14: Speed up your developments with Symfony2

Start to use Symfony2 and happy coding

Page 15: Speed up your developments with Symfony2

Want to give it a try?

Page 16: Speed up your developments with Symfony2

Symfony2 Philosophy

« Basically, Symfony2 asks you to convert a

Request into a Response »

Page 17: Speed up your developments with Symfony2

Request Handling

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

return new Response(sprintf('Hello %s!', $name)); }}

Page 18: Speed up your developments with Symfony2

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

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

Request Handling

Page 19: Speed up your developments with Symfony2

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

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

Request Handling

Template vars

Page 20: Speed up your developments with Symfony2

{% extends "SensioConferenceBundle::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 %}

View Rendering

Page 21: Speed up your developments with Symfony2

Twig is a modern template engine for PHP Fast

Concise and rich syntax Automatic output escaping Modern features Extensible Flexible

Twig Template Engine

Page 22: Speed up your developments with Symfony2

{% extends "SensioConferenceBundle::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

Template inheritance

Page 23: Speed up your developments with Symfony2

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

{% block body %}

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

{% block content %}{% endblock %}

{% endblock %} layout.html.t

wig

Template inheritance

Page 24: Speed up your developments with Symfony2

<!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: Speed up your developments with Symfony2

layout.html.twig

index.html.twig

base.html.twig

Template inheritance

Page 26: Speed up your developments with Symfony2

Smart URIs

Page 27: Speed up your developments with Symfony2

Native routing

mechanism

Bidirectional

Parameters

constraints support

Smart URIs

Page 28: Speed up your developments with Symfony2

class DefaultController extends Controller{ /** * @Route( * "/{year}/talk/{month}/{day}/{slug}", * requirements={ * "year"="\d{4}", * "month"="\d{2}", * "day"="\d{2}" * } * ) * @Template() */ public function showAction($slug, $day, $month, $year) { // ... }}

Page 29: Speed up your developments with Symfony2

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

Converts requests parameters to a Doctrine entity.

Doctrine Parameter Converter

Page 30: Speed up your developments with Symfony2

EasyDebuggin

g

http://www.flickr.com/photos/artelaphe/

Page 31: Speed up your developments with Symfony2

Symfony2 version

PHP environmen

t

Current environment

Current response

Recorded logs

Timers

Memory

Queries

The Web Debug Toolbar

Page 32: Speed up your developments with Symfony2

Exception Stack Trace

Page 33: Speed up your developments with Symfony2

Exception Stack Trace

Page 34: Speed up your developments with Symfony2

Recorded Logs

Page 35: Speed up your developments with Symfony2

The Profiler Application

Page 36: Speed up your developments with Symfony2

The Profiler Application

Page 37: Speed up your developments with Symfony2

http://www.flickr.com/photos/chanceprojects/

Page 38: Speed up your developments with Symfony2

Database Abstraction Layer

on top of PDO

Object Relational Mapper

Migrations support

Object Document Mapper

(MongoDB)

Object XML Mapper (XML

databases)

Doctrine2 Support

Page 39: Speed up your developments with Symfony2

Data Mapper Implementation

/** * @ORM\Entity() */class Talk{ /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id;

/** @ORM\Column(length=80) */ private $title;

/** @ORM\Column(type="text") */ private $synopsis;

/** @ORM\Column(type="datetime") */ private $schedule;

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

Page 40: Speed up your developments with Symfony2

Validation

Page 41: Speed up your developments with Symfony2

Validate POPOs

(properties & methods)

Easy configuration with

annotations

Easy to customize and

extend

Validation Framework

Page 42: Speed up your developments with Symfony2

namespace Sensio\Bundle\TodoBundle\Entity;

class Task{ private $name; private $dueDate;

public function getName() { return $this->name; }

public function setName($name) { $this->name = $name; }

public function getDueDate() { return $this->dueDate; }

public function setDueDate(\DateTime $dueDate = null) { $this->dueDate = $dueDate; }}

Page 43: Speed up your developments with Symfony2

namespace Sensio\Bundle\TodoBundle\Entity;use Symfony\Component\Validator\Constraints as Assert;

class Task{ /** * @Assert\NotBlank() * @Assert\MinLength(5) * @Assert\MaxLength(30) */ private $name;

/** * @Assert\NotBlank() * @Assert\Type() * @Assert\MaxLength(30) */ private $dueDate;

// ...}

Page 44: Speed up your developments with Symfony2

Forms Management

http://www.flickr.com/photos/miahz/

Page 45: Speed up your developments with Symfony2

Transparent layer on top of

your domain object

Native CSRF protection

Coupled to the Validation

framework

Twig integration

Forms Management

Page 46: Speed up your developments with Symfony2

namespace Sensio\Bundle\TodoBundle\Form;

use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilder;

class TaskType extends AbstractType{ public function buildForm(FormBuilder $builder, array $options) { $builder->add('task'); $builder->add('dueDate', null, array( 'widget' => 'single_text' )); }

public function getName() { return 'task'; }}

Page 47: Speed up your developments with Symfony2

use Sensio\Bundle\TodoBundle\Entity\Task;use Sensio\Bundle\TodoBundle\Form\TaskType;

public function newAction(){ $task = new Task(); $task->setName('Write a blog post'); $task->setDueDate(new \DateTime('tomorrow'));

$form = $this->createForm(new TaskType(), $task);

if ($request->getMethod() == 'POST') { $form->bindRequest($request);

if ($form->isValid()) { // save the task to the database...

return $this->redirect($this->generateUrl('success')); } }}

Page 48: Speed up your developments with Symfony2

{% extends 'SensioTodoBundle::layout.html.twig' %}

{% block content %}

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

{{ form_widget(form) }}

<input type="submit" value="Send!" />

</form>

{% endblock %}

Prototyping

Page 49: Speed up your developments with Symfony2

Functional Testing

http://www.flickr.com/photos/kenstein/

Page 50: Speed up your developments with Symfony2

Simulating an end-

user browsing

Crawling API for links

& forms

Testing internal

objects

Testing the response

Functional Testing

Page 51: Speed up your developments with Symfony2

class DefaultControllerTest extends WebTestCase{ public function testIndex() { $client = static::createClient();

$crawler = $client->request('GET', '/schedule');

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

$response = $client->getResponse();

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

Functional Testing

Page 52: Speed up your developments with Symfony2

HTTP Compliance (RFC2616)

Page 53: Speed up your developments with Symfony2

Expiration / Validation

Page 54: Speed up your developments with Symfony2

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

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

Expiration – Expires Header Field

Page 55: Speed up your developments with Symfony2

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

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

Expiration – Cache-Control Header Field

Page 56: Speed up your developments with Symfony2

InteractiveCode Generators

Page 57: Speed up your developments with Symfony2

$ php app/console generate:bundle

Page 58: Speed up your developments with Symfony2
Page 59: Speed up your developments with Symfony2

$ php app/console generate:doctrine:crud

Page 60: Speed up your developments with Symfony2

Native PHP Reverse Proxy Cache

Page 61: Speed up your developments with Symfony2

Varnish / Squid

Page 62: Speed up your developments with Symfony2

http://varnish-cache.org

Page 63: Speed up your developments with Symfony2

Edge Side Includes

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

Page 64: Speed up your developments with Symfony2

No ESI

Edge Side Includes Support

Page 65: Speed up your developments with Symfony2

<esi:include … />

With ESI

Edge Side Includes Support

Page 66: Speed up your developments with Symfony2

http://www.flickr.com/photos/bwop/

Page 67: Speed up your developments with Symfony2

<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <file source-language="en" datatype="plaintext" original="file.ext"> <body> <trans-unit id="1"> <source>Symfony2 is great</source> <target>J'aime Symfony2</target> </trans-unit> <trans-unit id="2"> <source>My name is %name%!</source> <target>Je m'appelle %name% !</target> </trans-unit> </body> </file></xliff>

Page 68: Speed up your developments with Symfony2

{% set message = 'Symfony2 is great' %}

{{ message|trans }}

{% set message = 'My name is %name%!' %}

{{ message|trans({'%name%': 'Hugo'}, "hello") }}

Translating Messages From Twig

Page 69: Speed up your developments with Symfony2

http://www.flickr.com/photos/cstein96/

Page 70: Speed up your developments with Symfony2

Roadmap for 2.1?

http://www.flickr.com/photos/mkrigsman/

Page 71: Speed up your developments with Symfony2

Thank You!