symfony 2
Post on 06-May-2015
4.019 Views
Preview:
TRANSCRIPT
Symfony 2Kris Wallsmith
@kriswallsmith
April 20, 2010
An evolution of symfony 1.x
• Same philosophy:
• Full stack
• Configurable
• Testable
• Components
• Brand new foundation
Don’t reinvent the wheel
• Doctrine
• PHPUnit
• SwiftMailer
• Zend_Cache
• Zend_Log
PHP 5.3
• Namespaces
• Anonymous functions / closures
• Late static binding
Symfony Components
• Standalone libraries
• Each with a specific scope
• components.symfony-project.org
Symfony Components• BrowserKit
• Console
• CssSelector
• DependencyInjection
• DomCrawler
• EventDispatcher
• OutputEscaper
• Process
• RequestHandler
• Routing
• Templating
• Yaml
Dependency Injection
• A method of supplying an external dependency
Dependency Injection
class User{ protected $session;
public function __construct(Session $session) { $this->session = $session; }}
DI Container
• A method of organizing dependencies
• Adds a configuration layer
• Dependency injection does not require a container!
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]]
DI Container
# in config_dev.yml
imports: - config.yml
parameters: mailer.transport.class: Zend_Mail_Transport_Null
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;
Event Dispatcher
• Implements the observer design pattern
• Similar to events in JavaScript
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); }}
Event Dispatcher
class Thumbnailer{ public function connect($dispatcher) { $dispatcher->connect(‘article.save’, array( $this, ‘generateArticleThumbnails’ )); }
public function generateArticleThumbnails($event) { // ... }}
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
The Symfony 2 sandbox
Live Demo
Follow us on GitHub
• symfony
• fabpot
• jwage
• kriswallsmith
• bschussek
• and 70+ other forks…
symfony-reloaded.org
top related