A Practical Introduction to Symfony2

Post on 15-Apr-2017

12.148 views 1 download

Transcript of A Practical Introduction to Symfony2

A Practical Introductionto Symfony2

Kris Wallsmith

PHP Matsuri • October 2, 2010

Saturday, October 2, 2010

@kriswallsmith

• Release Manager for symfony 1.3 & 1.4

• Doctrine contributor

• Senior Software Engineer at

• 10 years experience in web development

• Open source evangelist and international speaker

Saturday, October 2, 2010

the evolution of symfony

• Mojavi 3

• symfony (2007)

• Symfony2 (2011)

Saturday, October 2, 2010

a quick note for thecase-sensitive among us

Saturday, October 2, 2010

talk about “symfony”

Saturday, October 2, 2010

talk about “symfony 1”

Saturday, October 2, 2010

talk about “Symfony2”

Saturday, October 2, 2010

there is no “Symfony 1”

Saturday, October 2, 2010

there is no “symfony 2”

Saturday, October 2, 2010

there is no “Symfony 2”

Saturday, October 2, 2010

there is no “Symfony 2”

less search-friendly

{Saturday, October 2, 2010

Symfony2

Saturday, October 2, 2010

Symfony2#

Saturday, October 2, 2010

the evolution of symfony

• Mojavi 3

• symfony (2007)

• Symfony2 (2011)

Saturday, October 2, 2010

what’s old?

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

• don’t reinvent the wheel

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

• don’t reinvent the wheel

• loosely-coupled components

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

• don’t reinvent the wheel

• loosely-coupled components

• predictable conventions

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

• don’t reinvent the wheel

• loosely-coupled components

• predictable conventions

• highly configurable

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

• don’t reinvent the wheel

• loosely-coupled components

• predictable conventions

• highly configurable

• testable

Saturday, October 2, 2010

what’s old?

• same philosophy as symfony 1

• don’t reinvent the wheel

• loosely-coupled components

• predictable conventions

• highly configurable

• testable

• awesome developer tools

Saturday, October 2, 2010

what’s new?

Saturday, October 2, 2010

what’s new?

• PHP 5.3

• a brand new foundation

Saturday, October 2, 2010

what’s new?

• PHP 5.3

• a brand new foundation

• more smart, more lazy

Saturday, October 2, 2010

what’s new?

• PHP 5.3

• a brand new foundation

• more smart, more lazy

• REALLY REALLY FAST

Saturday, October 2, 2010

what’s new?

• PHP 5.3

• a brand new foundation

• more smart, more lazy

• REALLY REALLY FAST

Saturday, October 2, 2010

PHP 5.3

Saturday, October 2, 2010

PHP 5.3

• namespaces

• closures

Saturday, October 2, 2010

Saturday, October 2, 2010

HTTP\Message\Request

Saturday, October 2, 2010

HTTP\Message\Request

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

HTTP\Client\Request

Saturday, October 2, 2010

HTTP\Client\Request

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

closures

• anonymous functions

• lambda functions

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

Saturday, October 2, 2010

a new foundation

Saturday, October 2, 2010

anatomy of a Symfony2 request

Saturday, October 2, 2010

anatomy of a Symfony2 request

• Kernel

Saturday, October 2, 2010

anatomy of a Symfony2 request

• Kernel

• Request

Saturday, October 2, 2010

anatomy of a Symfony2 request

• Kernel

• Request

• Controller

Saturday, October 2, 2010

anatomy of a Symfony2 request

• Kernel

• Request

• Controller

• Response

Saturday, October 2, 2010

anatomy of a Symfony2 request

Saturday, October 2, 2010

anatomy of a Symfony2 request

• index.php creates a Kernel

Saturday, October 2, 2010

anatomy of a Symfony2 request

• index.php creates a Kernel

• the kernel creates a Request

Saturday, October 2, 2010

anatomy of a Symfony2 request

• index.php creates a Kernel

• the kernel creates a Request

• the kernel passes the Request to the ControllerResolver

Saturday, October 2, 2010

anatomy of a Symfony2 request

• index.php creates a Kernel

• the kernel creates a Request

• the kernel passes the Request to the ControllerResolver

• the ControllerResolver returns a callable

Saturday, October 2, 2010

anatomy of a Symfony2 request

• index.php creates a Kernel

• the kernel creates a Request

• the kernel passes the Request to the ControllerResolver

• the ControllerResolver returns a callable

• the kernel calls the callable

Saturday, October 2, 2010

anatomy of a Symfony2 request

• index.php creates a Kernel

• the kernel creates a Request

• the kernel passes the Request to the ControllerResolver

• the ControllerResolver returns a callable

• the kernel calls the callable

• the callable returns a Response

Saturday, October 2, 2010

Silexhttp://github.com/fabpot/Silex

Saturday, October 2, 2010

just enough, nothing more

Saturday, October 2, 2010

Saturday, October 2, 2010

switch ($_GET['pg'])case 'edit':// ...

Saturday, October 2, 2010

switch ($_GET['pg'])case 'edit':// ...

Saturday, October 2, 2010

Any volunteers to rewrite WordPress in Silex?

Saturday, October 2, 2010

quick tour

Saturday, October 2, 2010

frontend/src/web/

Saturday, October 2, 2010

frontend/ FrontendKernel.php cache/ config/ console logs/ phpunit.xml

Saturday, October 2, 2010

src/ autoload.php Application/ Bundle/ vendor/ symfony/ doctrine/

Saturday, October 2, 2010

web/ css/ images/ index.php index_dev.php js/

Saturday, October 2, 2010

../ MainBundle/ Controller/ MainBundle.php Resources/ config/ views/ Tests/

Saturday, October 2, 2010

How do I use it?

Saturday, October 2, 2010

# frontend/config/routing.ymlhomepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

// src/Application/MainBundle/Controller/MainController.phpnamespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

homepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

homepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

homepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

homepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

homepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class MainController extends Controller{ public function indexAction() { return $this->render('MainBundle:Main:index.php'); }}

homepage: pattern: / defaults: _controller: MainBundle:Main:index requirements: _method: get

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class BlogController extends Controller { public function showArticleAction($slug, $year) { // ... return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article)); }}

homepage: pattern: /:year/:slug defaults: _controller: MainBundle:Blog:showArticle requirements: year: \d{4}

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class BlogController extends Controller { public function showArticleAction($slug, $year) { // ... return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article)); }}

homepage: pattern: /:year/:slug defaults: _controller: MainBundle:Blog:showArticle requirements: year: \d{4}

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class BlogController extends Controller { public function showArticleAction($slug, $year) { // ... return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article)); }}

homepage: pattern: /:year/:slug defaults: _controller: MainBundle:Blog:showArticle requirements: year: \d{4}

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class BlogController extends Controller { public function showArticleAction($slug, $year) { // ... return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article)); }}

homepage: pattern: /:year/:slug defaults: _controller: MainBundle:Blog:showArticle requirements: year: \d{4}

Saturday, October 2, 2010

namespace Application\MainBundle\Controller;

class BlogController extends Controller { public function showArticleAction($slug, $year) { // ... return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article)); }}

homepage: pattern: /:year/:slug defaults: _controller: MainBundle:Blog:showArticle requirements: year: \d{4}

Saturday, October 2, 2010

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

Saturday, October 2, 2010

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article));

Saturday, October 2, 2010

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article));

Saturday, October 2, 2010

return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article));

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

Saturday, October 2, 2010

return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article));

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

Saturday, October 2, 2010

return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article));

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

Saturday, October 2, 2010

return $this->render('MainBundle:Blog:showArticle.php', array('article' => $article));

MainBundle/Resources/ config/ views/ Blog/ showArticle.php Main/ error404.php index.php layout.php

Saturday, October 2, 2010

MainBundle/Resources/ config/ views/ Blog/ showArticle.twig Main/ error404.php index.php layout.php

return $this->render('MainBundle:Blog:showArticle.twig', array('article' => $article));

Saturday, October 2, 2010

MainBundle/Resources/ config/ views/ Blog/ showArticle.twig Main/ error404.php index.php layout.php

return $this->render('MainBundle:Blog:showArticle.twig', array('article' => $article));

Saturday, October 2, 2010

TwigThe flexible, fast, and secure template language for PHP

http://twig-project.org

Saturday, October 2, 2010

symfony 1

Saturday, October 2, 2010

symfony 1

• templates

Saturday, October 2, 2010

symfony 1

• templates

• layouts

Saturday, October 2, 2010

symfony 1

• templates

• layouts

• slots

Saturday, October 2, 2010

symfony 1

• templates

• layouts

• slots

• components

Saturday, October 2, 2010

symfony 1

• templates

• layouts

• slots

• components

• partials

Saturday, October 2, 2010

symfony 1

• templates

• layouts

• slots

• components

• partials

• component slots

Saturday, October 2, 2010

fewer concepts, more power

Saturday, October 2, 2010

Symfony2

Saturday, October 2, 2010

Symfony2

• templates

Saturday, October 2, 2010

Symfony2

• templates

• slots

Saturday, October 2, 2010

Symfony2

• templates

• slots } fewer concepts, more power

Saturday, October 2, 2010

<?php $view->extend('MainBundle::layout.php') ?>

<?php foreach ($people as $person): ?>

<?php $view->render( 'MainBundle:Person:thumbnail', array('person' => $person) ) ?>

<?php endforeach; ?>

Saturday, October 2, 2010

<?php $view->extend('MainBundle::layout.php') ?>

<?php foreach ($people as $person): ?>

<?php $view->render( 'MainBundle:Person:thumbnail', array('person' => $person) ) ?>

<?php endforeach; ?>

decorate_with()

Saturday, October 2, 2010

<?php $view->extend('MainBundle::layout.php') ?>

<?php foreach ($people as $person): ?>

<?php $view->render( 'MainBundle:Person:thumbnail', array('person' => $person) ) ?>

<?php endforeach; ?>

Saturday, October 2, 2010

<?php $view->extend('MainBundle::layout.php') ?>

<?php foreach ($people as $person): ?>

<?php $view->render( 'MainBundle:Person:thumbnail', array('person' => $person) ) ?>

<?php endforeach; ?>

Saturday, October 2, 2010

<?php $view->extend('MainBundle::layout.php') ?>

<?php foreach ($people as $person): ?>

<?php $view->render( 'MainBundle:Person:thumbnail', array('person' => $person) ) ?>

<?php endforeach; ?>

include_partial()

Saturday, October 2, 2010

<?php $view->extend('MainBundle::layout.php') ?>

<?php foreach ($people as $person): ?>

<?php $view->render( 'MainBundle:Person:thumbnail', array('person' => $person) ) ?>

<?php endforeach; ?>

Saturday, October 2, 2010

helpers

Saturday, October 2, 2010

<?php $view['slots']->set('title', 'Hi!') ?>

<?php $view['slots']->start('sidebar') ?>

My awesome sidebar!

<?php $view['slots']->stop() ?>

Saturday, October 2, 2010

<?php $view['slots']->set('title', 'Hi!') ?>

<?php $view['slots']->start('sidebar') ?>

My awesome sidebar!

<?php $view['slots']->stop() ?>

Saturday, October 2, 2010

helpers are objects

Saturday, October 2, 2010

$view['javascripts']->add('script.js');echo $view['javascripts'];

echo $view['assets']->getUrl('logo.gif');

echo $view['router']->generate( 'person_show', array('username' => $username));

$view['actions']->output('MainBundle:Search:form');

use_javascript()

Saturday, October 2, 2010

$view['javascripts']->add('script.js');echo $view['javascripts'];

echo $view['assets']->getUrl('logo.gif');

echo $view['router']->generate( 'person_show', array('username' => $username));

$view['actions']->output('MainBundle:Search:form');

include_javascripts()

Saturday, October 2, 2010

$view['javascripts']->add('script.js');echo $view['javascripts'];

echo $view['assets']->getUrl('logo.gif');

echo $view['router']->generate( 'person_show', array('username' => $username));

$view['actions']->output('MainBundle:Search:form');

public_path()

Saturday, October 2, 2010

$view['javascripts']->add('script.js');echo $view['javascripts'];

echo $view['assets']->getUrl('logo.gif');

echo $view['router']->generate( 'person_show', array('username' => $username));

$view['actions']->output('MainBundle:Search:form');url_for()

Saturday, October 2, 2010

$view['javascripts']->add('script.js');echo $view['javascripts'];

echo $view['assets']->getUrl('logo.gif');

echo $view['router']->generate( 'person_show', array('username' => $username));

$view['actions']->output('MainBundle:Search:form');

include_component()

Saturday, October 2, 2010

dependency injection

Saturday, October 2, 2010

lazy objects

Saturday, October 2, 2010

if you want me to fooyou better give me the foo-er“

”Saturday, October 2, 2010

class User{ protected $session;

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

Saturday, October 2, 2010

class User{ protected $session;

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

Saturday, October 2, 2010

class User{ protected $session;

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

Saturday, October 2, 2010

$session = $this->getMock('Session') ->expects($this->any()) ->method('get') ->with('foo') ->will($this->returnValue('bar'));

// inject the mock object!$user = new User($session);

$this->assertEquals('bar', $user->getSessionVar('foo'));

Saturday, October 2, 2010

dependency injection container

• a configuration layer

• creates a "container" that manages the creation of objects

• “teach” the container

• using xml, yaml, php, ini(or some combination)

Saturday, October 2, 2010

services: session: class: Session

user: class: User arguments: - @session

Saturday, October 2, 2010

public function getUserService(){ if (isset($this->shared['user'])) return $this->shared['user'];

$user = new User( $this->getSessionService() );

$this->shared['user'] = $user;

return $user;}

Saturday, October 2, 2010

// get one service by name$container->get('user');

Saturday, October 2, 2010

// find many services by "tag"$c->findTaggedServiceIds('my_tag');

Saturday, October 2, 2010

services: foo_helper: class: Foo tags: - name: my_tag

Saturday, October 2, 2010

container parameters

Saturday, October 2, 2010

parameters: foo.class: Foo

services: foo: class: %foo.class%

Saturday, October 2, 2010

parameters: foo.class: Foo

services: foo: class: %foo.class%

Saturday, October 2, 2010

parameters: foo.class: Foo

services: foo: class: %foo.class%

Saturday, October 2, 2010

dependency injection extensionssemantic configuration

Saturday, October 2, 2010

doctrine.dbal: dbname: xxx user: xxx password: xxx

doctrine.orm: ~

Saturday, October 2, 2010

awesome developer tools

Saturday, October 2, 2010

Web Debug Toolbar

Saturday, October 2, 2010

Web Debug Toolbar

Saturday, October 2, 2010

Web Debug Toolbar

Saturday, October 2, 2010

Web Profiler

Saturday, October 2, 2010

Web Profiler

Saturday, October 2, 2010

Web Profiler

Saturday, October 2, 2010

Web Profiler

Saturday, October 2, 2010

Web Profiler

Saturday, October 2, 2010

Questions?

Saturday, October 2, 2010

OpenSky is Hiring!http://engineering.shopopensky.com

Please contact me if you're interested.

Saturday, October 2, 2010

OpenSky is Hiring!http://engineering.shopopensky.com

Please contact me if you're interested.

Saturday, October 2, 2010

symfony-reloaded.org

Saturday, October 2, 2010