Symfony 2.0 Intro

Post on 07-May-2015

7.130 views 1 download

description

A tech talk on Symfony 2.0 for San Francisco PHP/Symfony Meetup at CBS Interactive.

Transcript of Symfony 2.0 Intro

Dustin Whittle - @dustinwhittle

Symfony-Reloaded.org

What is Symfony?Symfony is a PHP 5.3 full-stack web framework. It is written with speed and flexibility in mind. It allows developers to build better and easier to maintain websites with PHP.

What is new in Symfony?

Everything!

It is Symfony not symfony

A fresh look at Symfony

Symfony2 is a complete rewrite of symfony 1.x

Fabien Potencier has been working on Symfony2 for years - almost as long as symfony 1.x

Focus is on Performance, Flexibility, and Extensibility

Cleaner implementations with less magic

Coming from symfony 1.x

If you are launching today, use symfony 1.4

No simple upgrade path to Symfony 2.0

If you are launching in six months use Symfony 2.0

Current release is alpha

The Foundation

PHP 5.3.2+ required

Leverage the latest features for a clean and simple API

Building on the shoulders of giants

ORM: Doctrine2 or Propel

Email: SwiftMailer

Testing: PHPUnit

Logging: Zend Framework

The Core

The Kernel

Bootstraps configuration + loads bundles

The Bundles

Almost all functionality is a bundle in Symfony2

FoundationBundle, DoctrineBundle, DoctrineMongoDBBundle, PropelBundle, SwiftmailerBundle, TwigBundle, ZendBundle

Use bundles to share features between projects

The Core

Dependency Injection Container

Configure dependencies in YAML or XML

Only use what you need when you need it

The Request Handler

Converts a request into a response

Can be highly optimized depending on requirements

Event Dispatcher

Take the quick tour

symfony-reloaded.org/learn

Getting started

Download the sandbox

http://github.com/symfony/symfony-sandbox

Doctrine 2.0

Doctrine2 features fully integrated

Database Abstraction Layer

Object Relational Mapper

Rewritten from scratch to focus on performance

Less magic

Configuring Doctrine DBAL

doctrine.dbal: default_connection: default connections: default: driver: PDOSqlite dbname: Symfony user: root password: null host: localhost port: ~ path: %kernel.data_dir%/symfony.sqlite event_manager_class: Doctrine\Common\EventManager configuration_class: Doctrine\DBAL\Configuration wrapper_class: ~ options: []

Using Doctrine Connections

class MyController extends DoctrineController{ public function indexAction() { $conn = $this->getDatabaseConnection('default'); // ... }}

Configuring Doctrine ORM

doctrine.orm: default_entity_manager: default cache_driver: apc # array, apc, memcache, xcache entity_managers: default: connection: default

Creating Doctrine Entities/** @Entity */class User{ /** * @Id @Column(type="integer") * @GeneratedValue */ private $id;

/** @Column(type="string", length=255) */ private $name;

public function getId() { return $this->id; }

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

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

Using Doctrine Entities

class MyController extends DoctrineController{ public function indexAction() { $em = $this->getEntityManager(); $query = $em->createQuery('select u from MyBundle:User u');

$users = $query->execute(); // ... }}

Using Doctrine Entities

class MyController extends DoctrineController{ public function indexAction() { $em = $this->getEntityManager(); $qb = $em->createQueryBuilder() ->select('u') ->from('MyBundle:User', 'u');

$query = $qb->getQuery(); $users = $query->execute(); // ... }}

Console commands implemented for improved developer workflow:

Ensure production settings

Clear metadata, query and result cache

Load data fixtures

Create and drop configured databases

Generate entities from mapping information

Generate new skeleton entities

Generate skeleton entity repository classes

Convert mapping information between formats

Doctrine 2.0 & MongoDB

Doctrine 2.0 supports Mongodb

MongoDB Object Document Mapper

Transparent persistence to MongoDB

Same architecture as ORM

Map a class as an entity and document

Configure Doctrine ODM

doctrine_odm.mongodb: default_document_manager: default cache_driver: array document_managers: default: connection: mongodb connections: mongodb: server: localhost/somedatabase

Mongo Documents

/** @Document */class User{ /** * @Id */ private $id;

/** @String */ private $name;

public function getId() { return $this->id; }

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

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

Using Doctrine/MongoDB

class MyController extends DoctrineController{ public function createAction() { $dm = $this->getDocumentManager(); $user = new User(); $user->setName('Jonathan H. Wage'); $dm->persist($user); $dm->flush(); // ... }}

Using Doctrine/MongoDB

class MyController extends DoctrineController{ public function indexAction() { $dm = $this->getDocumentManager(); $query = $dm->createQuery('User') ->where('username', 'jwage');

$user = $query->getSingleResult(); // ... }}

Symfony 2.0 Forms

Symfony 2.0 Testing

Uses PHPUnit 3.5.x

Adds custom web client + crawler

Symfony 2.0 Caching

Leverages ESI for ideal caching on the edge

Easy integration with any reverse proxy cache

Finding Documentation

Symfony Official Site

http://symfony-reloaded.org

The State of Symfony slides

http://www.slideshare.net/fabpot

Join the symfony-users@googlegroups.com

Want to contribute?

http://github.com/symfony/symfony