A Practical Introduction to Symfony2Presentation Transcript
A Practical Introduction
to 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 the
case-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
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
Silex
http://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
“ if you want me to foo
you 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