2. Fabien Potencier
• Serial entrepreneur and developer by passion
• Founder of Sensio
– Web Agency (France and USA)
– Since 1998
– 70 people
– Open-Source Specialists
– Big corporate customers
– Consulting, training, development, web design, … and more
– Sponsor of a lot of Open-Source projects
like symfony and Doctrine
3. Fabien Potencier
• Creator and lead developer of symfony…
• and creator and lead developer of some more:
– symfony components
– Swift Mailer : Powerful component based mailing library for PHP
– Twig : Fexible, fast, and secure template language for PHP
– Pirum : Simple PEAR Channel Server Manager
– Sismo : PHP continuous integration server
– Lime : Easy to use unit testing library for PHP
– Twitto : A web framework in a tweet
– Twittee : A Dependency Injection Container in a tweet
– Pimple : A small PHP 5.3 dependency injection container
4. Fabien Potencier
• Read my technical blog: http://fabien.potencier.org/
• Follow me on Twitter: @fabpot
• Fork my code on Github: http://github.com/fabpot/
5. symfony
• Full-stack framework (MVC architecture)
• symfony provides the infrastructure/tools needed for 95% of the web
projects
• Open-Source (MIT License) since 2005
• Based on
– 11 years of Sensio experience building websites
– Existing Open-Source projects
6. Documentation
• Books in several languages 2ND EDITION
2ND EDITION
The symfony 1.2 Reference Guide
• All the official documentation is released
Practical symfony
Practical The symfony 1.2
under an Open-Source license, and also symfony Reference Guide
Create professional web applications
available as printed books with PHP and symfony 1.2
Doctrine
symfony 1.2
Fabien Potencier
!"#$%&'(
F. Potencier
*+,-)"%&.'."(&!"#.#$.#
"#### / *+,-)"%&.'."(&!"#.#$.!
"#### /
Fabien Potencier
" %&'"(& !"##$#
)
)
Fabien Potencier
" %&'"(& !"##$!
9:;
les Cahiers
9:;
du
Programmeur
!"!"#$%&"
!"!"#$%&"
'('#!"#$%&"$%&'()*+,-./)*+ Mieux développer en PHP
0&123+,45678
avec Symfony et Doctrine
Doctrine
symfony 1.2
Fabien Potencier
Avec la contribution
!"#$%&'( de Hugo Hamon
F. Potencier
Le tutoriel
i
*+,-)"&'.(."$'!"#.$$.%
"#### / Jobeet enrich
et mis à jour
eur
par le créat
Fabien Potencier !
" &'("$' !"#$$%
)
de Symfony
The symfony 1.2 symfony!"#$%&
Reference Guide
symfony
The symfony 1.2
!"#$%&
9:;
9:;
Reference Guide
!"!"#$%&"
!"!"#$%&"
Fabien Potencier
Fabien Potencier
'('#!"#$%&"$%&'()*+,-./)*+
0&123+,45678
Propel
symfony 1.2
)*+,("&%-'-"$%!"#-$!-%
F. Potencier
"#### .
Fabien Potencier
Fabien Potencier
(
" &%'"$% !"#$!%
)*+,("&'-%-"$'!"#-$%-$
"#### .
(
Fabien Potencier
" &'%"$' !"#$%$
8. Very active community
Mailing-lists / forum / IRC
More than 750 plugins with about 1-2 new plugins a day
700 000 visitors per month on symfony-project.org
9. Used to build the largest websites
• Yahoo! Bookmarks sf-to.org/bookmarks
• Yahoo! Answers sf-to.org/answers
• delicious.com sf-to.org/delicious
• Dailymotion sf-to.org/dailymotion
10. Dailymotion is the second
most popular video sharing website
One of the top 30 websites in the world
59 million unique users in July 2009
12. Roadmap
• 1.0 – January 2007
• 1.1 – June 2008
• 1.2 – November 2008
• 1.3 – November 2009
• 1.4 – Last 1.X version
– same as 1.3 but with all deprecated features removed
• Version 2.0 – First alpha early 2010
13. “Enterprise” version
Version 1.0 LTS: Maintained for 3 years
Version 1.1, 1.2, 1.3: Maintained for 1 year
Version 1.4 LTS: Maintained for 3 years
Regular releases
– Bug and security fixes, compatibility with newer PHP versions
– No new feature (even small ones)
– Upgrading is simple and safe
16. Zend Framework
• Library of components with a MVC layer
• New BSD license
• Open-Source since 2007
How many of you use Zend Framework?
17.
18. How many of you use
Zend Framework in
symfony projects?
19. How many of you use
symfony in
Zend Framework
projects?
20. Using symfony and Zend Framework together?
• Both are plain PHP projects
• Both use Subversion
• Both use a PHP autoloader
• So, using both in a project is, well, trivial
21. Using symfony and Zend Framework together?
• Of course, people find it weird because both are MVC frameworks
• But both have more than just the MVC part
• Using them both in a project means
– Choosing one for the structure (controller, project organization, structure, and
configuration)
– Choosing the components/libraries, based on your needs and their strengths
22. Using symfony and Zend Framework together?
• I like this approach a lot because:
– I don't like to reinvent the wheel
– symfony itself started its life as a glue between existing components
– and symfony is still based on some third-party libraries
• Propel
• Doctrine
• Swift Mailer
• …
• Of course, I like it even more
if you use Zend Framework within a symfony project ;)
24. Install ZF into symfony
Put library/Zend/ files into lib/vendor/Zend
25. Setup autoloading
// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(array('sfDoctrinePlugin'));
set_include_path(sfConfig::get('sf_lib_dir').'/
vendor'.PATH_SEPARATOR.get_include_path());
require_once sfConfig::get('sf_lib_dir').'/vendor/Zend/
Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
}
}
26.
27. What people use?
• Pretty much everything except the MVC part:
– Zend_Search_Lucene
– Zend_OpenId
– Zend_Amf
– Zend_Feed
– Zend_Ldap
– Zend_Pdf
– Zend_Service
– Zend_Soap
28. Already well documented
2ND EDITION
2ND EDITION
• In the Jobeet tutorial
Practical symfony
• and Practical symfony book Practical
symfony
• Chapter 17 (symfony 1.3) explains Create professional web applications
with PHP and symfony 1.2
Doctrine
symfony 1.2
the configuration and how to use !"#$%&'(
F. Potencier
Zend_Search_Lucene
*+,-)"%&.'."(&!"#.#$.#
"#### /
) Fabien Potencier
" %&'"(& !"##$#
http://www.symfony-project.org/jobeet/1_3/Doctrine/en/17
31. A bit of history
symfony 1.0 (January 2007) started as a glue between existing
Open-Source libraries
32. A bit of history
symfony 1.1 (June 2008) was a big refactoring of the code base
– Decoupled but cohesive components: the symfony component like Forms,
Routing, Cache, YAML, ORMs, …
sfRequest sfRouting sfLogger sfI18N sfUser sfResponse
sfYAML sfDatabase sfForm sfEventDispatcher sfStorage sfCache sfOutputEscaper
sfValidator sfWidget sfCoreAutoload
platform
33. The symfony MVC framework is
based on a set of cohesive but
decoupled classes, the symfony
components
34. Symfony Components
• Standalone components
• Packaged individually
• Dedicated website for each component (with code and documentation)
– http://components.symfony-project.org/
• Dedicated Subversion and Git repository
– http://svn.symfony-project.com/components/
– http://github.com/fabpot
35. symfony components
• Extracted from symfony
– Event dispatcher
– YAML
– Output Escaping
• Written from scratch for Symfony 2
– Dependency Injection Container
– Request Handler
• We won’t duplicate effort with ZF
37. symfony components in a ZF project
• symfony components can make your ZF application better
– More configurable: symfony YAML
– More flexible: symfony Event Dispatcher
– Faster: symfony Dependency Injection
– More secure: symfony Output Escaper
• … and more fun of course
38. symfony components in a ZF project
• The examples in this presentation are just to get you started faster
• So, be creative with them. They open all kind of opportunities for your
next Zend Framework project
• And please, give me feedback, and tell me what you do with the
symfony components
39. YAML
• YAML is a human friendly data serialization standard for all
programming languages (implementations exist in Perl, Ruby, Python,
Java, …)
• YAML is a great format for your configuration files
• YAML files are as expressive as XML files and as readable as INI files
40. YAML
• Used in symfony for all configuration files
• Used in Doctrine for schema and fixtures
• Can be used in Zend Framework for the same purposes
42. Event Dispatcher
• Symfony Event Dispatcher is a PHP library that provides a lightweight
implementation of the Observer design pattern
• Based on the Cocoa Notification Center
• Used in symfony to provide hooks and allow customizations of default
behaviors sfRequest sfRouting sfLogger sfI18N sfUser sfResponse
– Inject the Web Debug Toolbar sfYAML sfDatabase sfForm sfEventDispatcher sfStorage sfCache sfOutputEscaper
– Inject methods into core Objects sfValidator sfWidget sfCoreAutoload
platform
– I18n management
– …
43. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function run()
{
require_once '/path/to/sfEventDispatcher.php';
$dispatcher = new sfEventDispatcher();
$event = new sfEvent(null, 'bootstrap.prerun');
$dispatcher->notify($event);
parent::run();
$event = new sfevent(null, 'bootstrap.postrun');
$dispatcher->notify($event);
}
}
44. // register a listener
$dispatcher->connect('event_name', array($this, 'callback'));
// notify an event
$event = new sfEvent($this, 'event_name', $parameters);
$dispatcher->notify($event);
$dispatcher->notifyUntil($event);
if (!$event->isProcessed())
{
// ...
}
$dispatcher->filter($event, $value);
$value = $event->getReturnValue();
45. Dependency Injection Container
• Symfony Dependency Injection is a PHP library that provides a
lightweight Dependency Injection Container
• … for classes that implement the Dependency Injection pattern
– This is the case for symfony classes
– This is mostly the case for Zend Framework classes
• Based on the Java Spring framework
• Configuration with PHP, YAML, or XML
46. « Dependency Injection is where components
are given their dependencies through their
constructors, methods, or directly into fields. »
http://www.picocontainer.org/injection.html
47. DI Hello World example
class Message
{
public function __construct(OutputInterface $output, array $options)
{
$this->output = $output;
$this->options = array_merge(array('with_newline' => false), $options);
}
public function say($msg)
{
$this->output->render($msg.($this->options['with_newline'] ? "n" : ''));
}
}
48. DI Hello World example
interface OutputInterface
{
public function render($msg);
}
class Output implements OutputInterface
{
public function render($msg)
{
echo $msg;
}
}
class FancyOutput implements OutputInterface
{
public function render($msg)
{
echo sprintf("033[33m%s033[0m", $msg);
}
}
49. DI Hello World example
$output = new FancyOutput();
$message = new Message($output, array('with_newline' => true));
$message->say('Hello World');
50. A DI container facilitates
objects description and object relationships,
configures and instantiates objects
51. DI Container Hello World example
$container = new sfServiceContainerBuilder();
$container->register('output', 'FancyOutput');
$container->
register('message', 'Message')->
setArguments(array(new sfServiceReference('output'), array('with_newline' => true)))
;
$container->message->say('Hello World!');
52. $message = $container->message;
Get the configuration for the message service
The Message constructor must be given an output service
Get the output object from the container
Create a Message object by passing the constructor arguments
53. $message = $container->message;
is roughly equivalent to
$output = new FancyOutput();
$message = new Message($output, array('with_newline' => true));!
54. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function getContainer()
{
if (null === $this->_container)
{
$this->setContainer($this->_initContainer());
}
return $this->_container;
}
protected function _initContainer()
{
require_once '/path/to/sfServiceContainerAutoloader.php';
sfServiceContainerAutoloader::register();
Example from Ben Eberlei (he rocks!): http://www.whitewashing.de/blog/articles/118
$container = new sfServiceContainerBuilder();
$loader = new sfServiceContainerLoaderFileXml($container);
$loader->load(dirname(__FILE__).'/configs/resources.xml');
return $container;
}
}
57. class GuestbookController extends Zend_Controller_Action
{
public function indexAction()
{
$guestbook = new Default_Model_Guestbook();
$this->view->entries = $guestbook->fetchAll();
$container = $this->getInvokeArg('bootstrap')->getContainer();
$mailer = $container->mailer;
}
}
58. Dependency Injection Container
• By default in ZF, new resources can be added to the container but cannot
be lazy-loaded
– All resources used by Zend_Application are loaded on every request
• By using symfony Service Container, the resources are lazy-loaded
– Instances and their dependencies are created the first time you get them
• Interesting for resources like DB
59. Output Escaper
• Automatic XSS Protection for your templates
• Wraps your template variables
• Works for strings, arrays, objects, …
• Works for deep method calls
62. Conclusion
• Use the right tool for the job
• The PHP community have a lot of great projects
– Zend Framework
– Symfony
– eZ Components
– Doctrine
– Swift Mailer
– Twig
– … and much more
63. symfony-live.com
with Matthew Weier O’Pheinney
I will reveal the first alpha release of Symfony 2.0!
64. symfony-live.com
with Matthew Weier O’Pheinney
… with Matthew Weier O'Phinney as a special guest