Symfony2 - from the trenches


Published on

This talk represents the combined experience from several web development teams who have been using Symfony2 since months already to create high profile production applications. The aim is to give the audience real world advice on how to best leverage Symfony2, the current rough spots and how to work around them. Aside from covering how to implement functionality in Symfony2, this talk will also cover topics such as how to best integrate 3rd party bundles and where to find them as well as how to deploy the code and integrate into the entire server setup.

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Symfony2 - from the trenches

  1. 1. Symfony2 - From the Trenches by Lukas Kahwe Smith, Kris Wallsmith, Thibault Duplessis, Jeremy Mikola, Jordi Boggiano, Jonathan H. Wage, Bulat Shakirzyanov
  2. 2. AgendaGetting Setup CachingCode Flow Performance TipsDependency Injection Asset ManagementConfiguration Choices TestingController Choices DeploymentApplication Choices Third Party BundlesDoctrine Resources
  3. 3. Getting setupPHP 5.3+ with ext/intl (compat lib is in the works) Read check.php for details (dev/prod php.inis from Liip) Using OSX? php53-intl from liangzhenjing or build-entropy-php from chregu Blog post on installing PHP 5.3 with intl from Justin HilemanInitial setup symfony-sandbox symfony-bootstrap Symfony2ProjectRead the Coding Style Guide (Code Sniffer Rules)Managing external dependencies Submodule: not everything is in git (svn, mercurial, etc.) Vendor install/update scripts: risk of getting out of sync MR (not cross platform)
  4. 4. Code Flow1. Frontend Controller (web/app[_dev].php) Loads autoloader Creates/boots kernel Creates request (from globals) and passes to kernel2. Kernel Loads app config (app/config/config_[prod|dev|test]) Resolves URL path to a controller (go to 3) Outputs response returned by the controller3. Controller Loads model and view Potentially creates a sub-request (go to 2) Creates response and returns it
  5. 5. Execution Flow
  6. 6. <?xml version="1.0" encoding="utf-8" ?><container xmlns="" xmlns:xsi=""> <parameters> <parameter key="sitemap.class">BundleAvalancheSitemapBundleSitemap</parameter> </parameters> <services> <service id="sitemap" class="%sitemap.class%" /> </services></container> Describe Your Services
  7. 7. <?phpclass cachedDevDebugProjectContainer extends Container{ /** * Gets the sitemap service. * * @return BundleAvalancheSitemapBundleSitemap */ protected function getSitemapService() { return $this->services[sitemap] = new BundleAvalancheSitemapBundleSitemap(); } /** * Gets the default parameters. * * @return array An array of the default parameters */ protected function getDefaultParameters() { return array( sitemap.class => BundleAvalancheSitemapBundleSitemap ); }} Service Definitions Are Dumped to Raw PHP
  8. 8. Service Container (aka DIC)Benefits: No performance loss Lazy instantiation Readable service configurationsGotchas: Can become hard to work with if the DI extension tries to do too much Be aware of circular dependencies Might lead to code that cannot be used outside of DIC
  9. 9. <?phpclass SomeClass{ private $container; public function __construct(ContainerInterface $container) <service id="some_service" class="SomeClass"> { <argument type="service" id="service_container" /> $this->container = $container; </service> } <!-- or --> // or <service id="some_service" class="SomeClass"> public function setContainer(ContainerInterface $container) <call method="setContainer"> { <argument type="service" id="service_container" /> $this->container = $container; </call> } </service> public function getDocumentManager() { return $this->container->get(document_manager); }} Container Injection
  10. 10. <?phpclass SomeClass{ private $documentManager; public function __construct(DocumentManager $documentManager) { $this->documentManager = $documentManager; } public function getDocumentManager() { return $this->documentManager; }}<service id="some_service" class="SomeClass"> <argument type="service" id="document_manager" /></service> Constructor Injection
  11. 11. <?phpclass SomeClass{ private $documentManager; public function setDocumentManager(DocumentManager $documentManager) { $this->documentManager = $documentManager; } public function getDocumentManager() { return $this->documentManager; }}<service id="some_service" class="SomeClass"> <call method="setDocumentManager"> <argument type="service" id="document_manager" /> </call></service> Setter Injection
  12. 12. <?phpinterface SomeInterface{ function setDocumentManager(DocumentManager $documentManager);}class SomeClass implements SomeInterface{ private $documentManager; public function setDocumentManager(DocumentManager $documentManager) { $this->documentManager = $documentManager; } public function getDocumentManager() { return $this->documentManager; }}<interface id="some_service" class="SomeInterface"> <call method="setDocumentManager"> <argument type="service" id="document_manager" /> </call></interface><service id="some_service" class="SomeClass" /> Interface Injection
  13. 13. Configuration ChoicesSymfony supports XML, YAML and PHP for configuration YAML and PHP uses underscore to separate words XML uses dashes to separate words XML attributes usually map to array values in YAML/PHP YAML merge key syntax to reuse pieces within a file XSD-aware editors provide auto-completion/validationXML is recommended for Bundle/DI configurationYAML is recommended for application configurationBundle extensions can optionally utilize the Configcomponent to normalize/parse configurations in any format See FrameworkExtension, SecurityExtension, TwigExtension
  14. 14. Controller ChoicesDefining Controllers as services is optional Non-service controllers must use container injection Create a Bundle extension to load Bundle servicesIts recommended to not extend from the base Controller The base controller is mainly a tool for beginners It provides convenience methods that invoke services, such as generateUrl(), redirect(), render()
  15. 15. Application ChoicesSecurity system makes it possible to have just oneapplication for both frontend and admin backendLocation of AppKernel is totally flexible, just update thefrontend controllers accordinglyLarge projects should use multiple applications Better separation when multiple teams work Facilitate step-by-step updating and refactoring For example: main, mobile, API, admin
  16. 16. Doctrine ExamplesRetrieve references to entity/document without DB queriesUsing raw SQL queries with Doctrine2 ORMSimple search engine with Doctrine MongoDB ODM
  17. 17. Retrieving References w/o DB Queries$tags = array(baseball, basketball);foreach ($tags as $tag) { $product->addTag($em->getReference(Tag, $tag));}
  18. 18. Raw SQL Queries$rsm = new ResultSetMapping;$rsm->addEntityResult(User, u);$rsm->addFieldResult(u, id, id);$rsm->addFieldResult(u, name, name);$query = $this->_em->createNativeQuery(SELECT id, nameFROM users WHERE name = ?, $rsm);$query->setParameter(1, romanb);$users = $query->getResult();
  19. 19. Simple Search Engineinterface HasKeywords{ function setKeywords(); function getKeywords();}
  20. 20. Simple Search Engine/** @mongodb:EmbeddedDocument */class Keyword{ // ... /** @mongodb:String @mongodb:Index */ private $keyword; /** @mongodb:Integer */ private $score; // ...}
  21. 21. Simple Search Engine/** @mongodb:Document */class Product implements HasKeywords{ /** @mongodb:Id */ private $id; /** @mongodb:String */ private $title; /** @mongodb:EmbedMany(targetDocument="Keyword") */ private $keywords = array(); // ...}
  22. 22. Simple Search Engineclass KeywordListener{ public function preUpdate(PreUpdateEventArgs $eventArgs) { $entity = $eventArgs->getEntity(); if ($entity instanceof HasKeywords) { $entity->setKeywords($this->buildKeywords($entity)); } } private function buildKeywords(HasKeywords $entity) { $keywords = array(); // build keywords from title, description, etc. return $keywords; }}
  23. 23. Simple Search Engine// find products by keyword$products = $dm->createQueryBuilder() ->field(keywords.keyword)->all($keywords) ->getQuery() ->execute();
  24. 24. Doctrine in the Real WorldGo see Jon Wages talk at later today!
  25. 25. Database MigrationsDeploy DB schema changes before the codePrevent DB schema BC breaksUse DoctrineMigrationBundle app/console doctrine:migrations:diff app/console doctrine:migrations:migrate Do not use entities in migration scripts ever!Write fixtures as migrations or make the fixtures able toupdate existing data gracefully app/console doctrine:data:load --fixtures=app/fixtures
  26. 26. Different Content Areas of a Page
  27. 27. Caching with Edge Side IncludesSymfony2 provides support for Edge Side Includes (ESI) Proxy assembles page from snippets of HTML Snippets can have different cache rules Develop without ESI, test with Symfony2 internal ESI proxy, deploy using ultra-fast Varnish ProxyBreak up page into different controller actions based oncache invalidation rules Do not worry about overhead from multiple render calls Never mix content that has different cache timeouts Consider caching user specific content in the client Varnish Reverse Proxy Super fast, PHP cannot match its performance Cache full pages for anonymous users Not just for HTML, also useful for JSON/XML APIs
  28. 28. Page Rendered Behind a Reverse Proxy
  29. 29. Asset ManagementGo see Kris Wallsmiths talk next!
  30. 30. Performance TipsDump routes to Apache rewrite rules app/console router:dump-apacheWrite custom cache warmersDo not explicitly inject optional services to controllers If your controller receives many services, which are optional or unused by some actions, thats probably a hint that you should break it up into multiple controllersDo minimal work in the controller, let templates pulladditional data as neededUse a bytecode cache with MapFileClassLoader
  31. 31. TestingSymfony2 rocks for unit and functional testing Dependency Injection, core classes have interfaces (easy mocking) No base classes, no static dependencies, no ActiveRecord Client fakes "real" requests for functional testing (BrowserKit component)Functional Testing Pros: tests configuration, tests API not implementationUnit Testing Pros: pinpoints issues, very directed testing Functional testing is recommended for controller actions Symfony2 provides WebTestCase and BrowserKit Unit testing for complex algorithms, third party APIs too hard to mockUse LiipFunctionalTesting to load fixtures, validate HTML5
  32. 32. DeploymentDebian style (aka Liip Debian Packager) Write a manifest in YAML Build Debian packages with MAKE Install with apt-get install Server specific settings are asked during install, change later with dpkg-reconfigure Maintain a global overview of all application dependencies in case of (security) updates Watch Lukas unconference talk later today!Fabric (used at OpenSky) Repository managed with git-flow Clone tagged release branch, init submodules Fix permissions (e.g. cache, log), delete dev/test controllers Replace password/API-key placeholders with prod values in config files Upload in parallel to production nodes, swap "current" symlink
  33. 33. Third Party Bundles @weaverryan Ryan WeaverHeres a new years resolution: to *always*work on an existing Symfony2 bundle andnever recreate my own. #focus #teamwork27 Dec
  34. 34. Third Party BundlesMany vendors have already published bundles: FriendsOfSymfony ( UserBundle (forked from knplabs DoctrineUserBundle) FacebookBundle (forked from kriswallsmith) Liip ( FunctionalTestBundle ViewBundle OpenSky ( LdapBundle Sonata ( AdminBundleAdditionally, a couple sites currently index community bundles:
  35. 35. Third Party BundlesBundles should follow the best practicesNo version-tagging or official package manager (yet)Use bundles by adding git submodules to your projectMaintain your own fork and "own" what you useNot all bundles are equally maintained Symfony2 API changes => broken bundles If you track symfony/symfony, learn to migrate bundlesAvoid rewriting a bundles services/parameters directly The bundles DI extension should allow for such configuration; if not, submit a pull request If absolutely necessary, a CompilerPass is cleaner
  36. 36. Contributing to Third Party Bundles Similar to Symfony2s own patch guidlines Fork and add remote repository Merge regularly to keep up-to-date Avoid committing directly to your master Merges from upstream should be fast-forwards Once upstream changes are stable, bump your projects submodule pointer
  37. 37. Contributing to Third Party Bundles Create branches for patches and new features Cant wait to use this in your project? Temporarily change your project submodule to point to your branch until your pull request is accepted. Help ensure that your pull request merges cleanly Create feature branch based on upstreams master Rebase or merge upstreams master when finished
  38. 38. Contributing to Third Party Bundles Was your pull request accepted? Congratulations! Dont merge your feature branch into master! Doing so would cause your master to divert Merge upstreams master into your master Delete your feature branch Update your projects submodule to point to master
  39. 39. ResourcesIf you want to jump in and contribute: you are still fuzzy on Dependency Injection: you keep up with Symfony2 repository on github:
  40. 40. Application Using Dependency Injection
  41. 41. Circular Dependency Example
  42. 42. Dependency InjectionAll objects are instantiated in one of two ways: Using the "new" operator Using an object factoryAll objects get collaborators in one of two ways Passed to object constructor Set using a setter
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.