how i built a reusable symfony2 taggingbundle
TRANSCRIPT
- 1. Willkommen
- 2. Anti-Pattern #1 Reusage by copy & paste Every time you do this, a baby seal dies Photo by Samuel Blanc via Wikimedia Commons [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)]
- 3. Scenario Community web application Users seek other users by interests How to input data?
- 4. I can haz tagz? Illustration by Kimhunghan via Wikimedia Commons [CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0)] Backend fpn/tag-bundle Frontend max-favilli/tagmanager + something in between
- 5. fpn/tag-bundle Actually a Doctrine Extension in a Bundle Pro Provides two new Entites: Tag & Tagging TagManager handles model access Con Some code to copy into our Entities Lots of code to copy into our Controllers
- 6. max-favilli/tagmanager jQuery plugin working nicely with twitter bootstrap Pro Basically just two form inputs Typeahead optional Con Not a composer project
- 7. Missing Puzzle Pieces Form Widget Data Transformer Form Type
- 8. Form Widget Two input fields visible for UI hidden for tagA, tagB, tagC Embed JavaScript & CSS Initialize JS
- 9. Data Transformer transform() convert Tags to a, b, c reverseTransform() convert tagA, tagB, tagC to Tags create Tags, if not existing needs TagManager
- 10. Form Type Provide name for widget Called in Application FormTypes Gets TagManager injected Defines DataTransformer
- 11. Save the baby seals Entity Trait TaggableSubstriber make it reusable
- 12. Entity Trait avoid copy & paste for each functional Entity classes reference TagManager after loading the Entity to inform TagManager during flush
- 13. TaggableSubscriber postLoad() attach TagManager to Entity postPersist() TagManager->saveTagging($entity) onFlush() save dirty Tags in TagManager Obsoletes code in Controllers
- 14. Make it reusable Dedicated Bundle Create composer project But how to include external JS & CSS?
- 15. Component RobLoach/component-installer Add package to composer.json Configure components-dir Run composer update See web/bundles
- 16. Live Demo
- 17. // TODO AppKernel: remove FPN bundle config.yml: remove FPN configuration Handling typeahead requests Configure tagmanager via Symfony Documentation :(
- 18. (re-)use it! http://github.com/fogs/TaggingBundle