Symfony2 revealed

download Symfony2 revealed

of 135

  • date post

    06-May-2015
  • Category

    Technology

  • view

    35.761
  • download

    0

Embed Size (px)

description

Symfony2 revealed

Transcript of Symfony2 revealed

  • Fabien Potencier

  • A bit of history

  • symfony 1.0 January 2007 Started as a glue between existing Open-Source libraries: Mojavi (heavily modified), Propel, Prado i18n,

    Borrowed concepts from other languages and frameworks: Routing, CLI, functional tests, YAML, Rails helpers

    Added new concepts to the mix Web Debug Toolbar, admin generator, configuration cascade,

  • symfony 1.2 November 2008 Based on decoupled but cohesive components

    Forms, Routing, Cache, YAML, ORMs,

    Controller still based on Mojavi

    View, Filter Chain,

  • symfony 1.4 November 2009 Added some polish on existing features

    Removed the support for deprecated features

    Current LTS release, maintained until late 2012

  • Symfony Components YAML Dependency Injection Container Event Dispatcher Templating Routing Console Output Escaper Request Handler

  • What is Symfony 2?

  • Symfony 2 is the next version of the symfony framework

    except Symfony now takes a S instead of a s

  • Talk about Symfony 2

    or symfony 1

  • To make it clear: Symfony 1

    does not make any sense

  • symfony 2 does not make more sense

  • Symfony 2

  • Same philosophy, just better

  • MVC

  • hmmm, now that I think about it

  • its now probably more a Fabiens style framework

    than anything else

  • Highly configurable Highly extensible

    Same Symfony Components Same great developer tools

    Full-featured

  • Ok, but why a major version then?

  • Symfony 2 has a brand new

    low-level architecture

  • PHP 5.3

  • A Quick Tour

  • Hello !

    Layout

  • Helpers are objects

  • hello: pattern: /hello/:name defaults: _bundle: HelloBundle _controller: Hello _action: index

  • hello: pattern: /hello/:name defaults: _bundle: HelloBundle _controller: Hello _action: index

    namespace Application\HelloBundle\Controller;

    class HelloController extends Controller { public function indexAction($name) { // ... } }

  • hello: pattern: /hello/:name defaults: _bundle: HelloBundle _controller: Hello _action: index

    namespace Application\HelloBundle\Controller;

    class HelloController extends Controller { public function indexAction($name) { // ... } }

  • hello: pattern: /hello/:name defaults: _bundle: HelloBundle _controller: Hello _action: index

    namespace Application\HelloBundle\Controller;

    class HelloController extends Controller { public function indexAction($name) { // ... } }

  • hello: pattern: /hello/:name defaults: _bundle: HelloBundle _controller: Hello _action: index

    namespace Application\HelloBundle\Controller;

    class HelloController extends Controller { public function indexAction($name) { // ... } }

  • hello: pattern: /hello/:name defaults: _bundle: HelloBundle _controller: Hello _action: index

    namespace Application\HelloBundle\Controller;

    class HelloController extends Controller { public function indexAction($name) { // ... } }

  • hello: pattern: /hello/:year/:month/:slug defaults: _bundle: HelloBundle _controller: Hello _action: index

    namespace Application\HelloBundle\Controller;

    class HelloController extends Controller { public function indexAction($slug, $year) { // ... } }

  • Extremely Configurable

  • Dependency Injection Container

  • Replaces a lot of symfony 1 things sfConfig

    All config handlers sfProjectConfiguration / sfApplicationConfiguration

    sfContext (No Singleton anymore) The configuration cache system

    and some more

  • in one easy-to-master

    unified and cohesive package

  • Thanks to the DIC, Configuration has never been

    so easy and so flexible

  • Name your configuration files the way you want

  • Store them where you want

  • Use PHP, XML, YAML, or INI

  • $configuration = new BuilderConfiguration(); $configuration->addResource(new FileResource(__FILE__));

    $configuration ->mergeExtension('web.user', array('default_culture' => 'fr', 'session' => array('name' => 'SYMFONY', 'type' => 'Native', 'lifetime' => 3600)))

    ->mergeExtension('doctrine.dbal', array('dbname' => 'sfweb', 'username' => 'root'))

    ->mergeExtension('web.templating', array('escaping' => 'htmlspecialchars', 'assets_version' => 'SomeVersionScheme'))

    ->mergeExtension('swift.mailer', array('transport' => 'gmail', 'username' => 'fabien.potencier', 'password' => 'xxxxxx')) ;

    PHP

  • web.user: default_culture: fr

    session: { name: SYMFONY, type: Native, lifetime: 3600 }

    web.templating: escaping: htmlspecialchars assets_version: SomeVersionScheme

    doctrine.dbal: { dbname: sfweb, username: root, password: null }

    swift.mailer: transport: gmail username: fabien.potencier password: xxxxxxxx

    YAML

  • XML

  • $configuration->mergeExtension('swift.mailer', array( 'transport' => 'gmail', 'username' => 'fabien.potencier', 'password' => 'xxxxxx', ));

    PHP

  • swift.mailer: transport: gmail username: fabien.potencier password: xxxxxxxx

    YAML

  • XML

  • XML

  • Inherit them as much as you want

  • Mix and match configuration files written in any

    format

    useful when using third-party plugins

  • Mix and match formats

  • You choose the format you want

    Pros Cons XML validation

    IDE completion & help verbose (not that much)

    YAML concise simple to read easy to change

    needs the YAML component no validation no IDE auto-completion

    PHP flexible more expressive

    no validation

  • Store sensitive settings outside of your project

  • SetEnv SYMFONY__DOCTRINE__DBAL__PASSWORD "foobar"

    in a .htaccess or httpd.conf file

    %doctrine.dbal.password%

  • Semantic Configuration

  • XML

  • XML

  • Swift_Mailer Swift_Transport_EsmtpTransport

    smtp.gmail.com 25 ssl fabien.potencier xxxxxx login swift_init.php

    %swiftmailer.init_file%

    %swiftmailer.transport.smtp.host% %swiftmailer.transport.smtp.port% %swiftmailer.transport.smtp.encryption% %swiftmailer.transport.smtp.username% %swiftmailer.transport.smtp.password% %swiftmailer.transport.smtp.auth_mode%

    XML

  • Creating DIC extensions is insanely simple

  • Very Fast thanks to a Smart

    Caching mechanism it always knows when to flush the cache

  • /** * Gets the 'swiftmailer.mailer' service. * * This service is shared. * This method always returns the same instance of the service. * * @return Swift_Mailer A Swift_Mailer instance. */ protected function getSwiftmailer_MailerService() { if (isset($this->shared['swiftmailer.mailer'])) return $this->shared['swiftmailer.mailer'];

    $instance = new Swift_Mailer($this->getSwiftmailer_Transport_SmtpService());

    return $this->shared['swiftmailer.mailer'] = $instance; }

    PHPDoc for auto-completion

    As fast as it could be

  • The DIC can manage ANY PHP object (POPO)

  • Plugins

  • or Bundles

  • Plugins are first-class citizens They are called Bundles

  • Everything is a bundle Core features

    Third-party code Application code

  • app/ src/ web/

  • app/ AppKernel.php cache/ config/ console logs/

  • src/ autoload.php Application/ Bundle/ vendor/ doctrine/ swiftmailer/ symfony/ zend/

  • web/ index.php index_dev.php

  • .../ SomeBundle/ Bundle.php Controller/ Model/ Resources/ config/ views/

  • public function registerBundleDirs() { return array( 'Application' => __DIR__.'/../src/Application', 'Bundle' => __DIR__.'/../src/Bundle', 'Symfony\\Framework' => __DIR__.'/../src/vendor/symfony/src/Symfony/Framework', ); }

  • $this->render('SomeBundle:Hello:index', $params)

  • hello: pattern: /hello/:name defaults: { _bundle: SomeBundle, ... }

  • SomeBundle can be any of

    Application\SomeBundle Bundle\SomeBundle Symfony\Framework\SomeBundle

  • Less concepts but more powerful ones

  • symfony 1 View Layer templates

    layouts slots

    components partials

    component slots

  • Symfony 2 View Layer

    templates slots

  • A layout is just another template with _content as a special slot

    A partial is just a template you embed in another one

    A component is just another action embedded in a template

  • Big and Small Improvements

  • multiple level of layouts

  • partials can be decorated!

  • Better Logs

  • INFO: Matched route "blog_home" (parameters: array ( '_bundle' => 'BlogBundle', '_controller' =&g