symfony 2

22
Symfony 2 Kris Wallsmith @kriswallsmith April 20, 2010

Upload: kris-wallsmith

Post on 06-May-2015

4.019 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Symfony 2

Symfony 2Kris Wallsmith

@kriswallsmith

April 20, 2010

Page 2: Symfony 2

An evolution of symfony 1.x

• Same philosophy:

• Full stack

• Configurable

• Testable

• Components

• Brand new foundation

Page 3: Symfony 2

Don’t reinvent the wheel

• Doctrine

• PHPUnit

• SwiftMailer

• Zend_Cache

• Zend_Log

Page 4: Symfony 2

PHP 5.3

• Namespaces

• Anonymous functions / closures

• Late static binding

Page 5: Symfony 2
Page 6: Symfony 2
Page 7: Symfony 2

Symfony Components

• Standalone libraries

• Each with a specific scope

• components.symfony-project.org

Page 8: Symfony 2

Symfony Components• BrowserKit

• Console

• CssSelector

• DependencyInjection

• DomCrawler

• EventDispatcher

• OutputEscaper

• Process

• RequestHandler

• Routing

• Templating

• Yaml

Page 9: Symfony 2

Dependency Injection

• A method of supplying an external dependency

Page 10: Symfony 2

Dependency Injection

class User{ protected $session;

public function __construct(Session $session) { $this->session = $session; }}

Page 11: Symfony 2

DI Container

• A method of organizing dependencies

• Adds a configuration layer

• Dependency injection does not require a container!

Page 12: Symfony 2

DI Container# in config.yml

parameters: mailer.username: foo mailer.password: bar mailer.class: Zend_Mail mailer.transport.class: Zend_Mail_Transport_Smtp

services: mail.transport: class: %mailer.transport.class% arguments: - smtp.gmail.com - { auth: login, username: %mailer.username%, password: %mailer.password%, ssl: ssl, port: 465 } shared: false mailer: class: %mailer.class% calls: - [setDefaultTransport, [@mail.transport]]

Page 13: Symfony 2

DI Container

# in config_dev.yml

imports: - config.yml

parameters: mailer.transport.class: Zend_Mail_Transport_Null

Page 14: Symfony 2

DI Container

use Symfony\Components\DependencyInjection as DI;use Symfony\Components\DependencyInjection\Loader;

$container = new DI\Container();

$loader = new Loader\YamlFileLoader($container);$loader->load(‘config_dev.yml’);

$mailer = $container->mailer;

Page 15: Symfony 2

Event Dispatcher

• Implements the observer design pattern

• Similar to events in JavaScript

Page 16: Symfony 2

Event Dispatcheruse Symfony\Components\EventDispatcher\Event;

class Article{ protected $dispatcher;

public function __construct($dispatcher) { $this->dispatcher = $dispatcher; }

public function save() { // ... $event = new Event($this, ‘article.save’); $this->dispatcher->notify($event); }}

Page 17: Symfony 2

Event Dispatcher

class Thumbnailer{ public function connect($dispatcher) { $dispatcher->connect(‘article.save’, array( $this, ‘generateArticleThumbnails’ )); }

public function generateArticleThumbnails($event) { // ... }}

Page 18: Symfony 2

The Symfony 2 sandbox

•curl -L http://bit.ly/sf2sbox > sandbox.tgz

•tar xzf sandbox.tgz

•cd sandbox

•chmod a+w hello/cache/ hello/logs

•chmod a+x hello/console

Page 19: Symfony 2

The Symfony 2 sandbox

Page 20: Symfony 2

Live Demo

Page 21: Symfony 2

Follow us on GitHub

• symfony

• fabpot

• jwage

• kriswallsmith

• bschussek

• and 70+ other forks…

Page 22: Symfony 2

symfony-reloaded.org