Willkommen
Anti-Pattern #1
Reusage by copy & paste
Every time
you do this,
a baby seal
dies
Photo by Samuel Blanc via Wikimedia Commo...
Scenario
Community web application
Users seek other users by
interests
How to input data?
I can haz tagz?
Illustration by Kimhunghan via Wikimedia Commons
[CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/...
fpn/tag-bundle
Actually a Doctrine Extension in a Bundle
Pro
•Provides two new Entites: Tag & Tagging
•TagManager handles ...
max-favilli/tagmanager
jQuery plugin
working nicely with twitter bootstrap
Pro
• Basically just two form inputs
• Typeahea...
Missing Puzzle Pieces
Form Widget
Data Transformer
Form Type
Form Widget
Two input fields
• visible for UI
• hidden for „tagA, tagB, tagC“
Embed JavaScript & CSS
Initialize JS
Data Transformer
transform()
• convert Tags to „a, b, c“
reverseTransform()
• convert „tagA, tagB, tagC“ to Tags
• create ...
Form Type
Provide name for widget
Called in Application FormTypes
Gets TagManager injected
Defines DataTransformer
Save the baby seals
Entity Trait
TaggableSubstriber
make it reusable
Entity Trait
avoid copy & paste for each
functional Entity classes
reference TagManager
• after loading the Entity
• to in...
TaggableSubscriber
postLoad()
• attach TagManager to Entity
postPersist()
• TagManager->saveTagging($entity)
onFlush()
• s...
Make it reusable
Dedicated Bundle
Create composer project
But how to include
external JS & CSS?
Component
RobLoach/component-installer
Add package to composer.json
Configure components-dir
Run composer update
See web/b...
Live Demo
// TODO
AppKernel: remove FPN bundle
config.yml: remove FPN configuration
Handling typeahead requests
Configure tagmanager...
(re-)use it!
http://github.com/fogs/TaggingBundle
Nächste SlideShare
Wird geladen in ...5
×

How I Built A Reusable Symfony2 TaggingBundle

1,608

Published on

In this talk I gave at the Symfony User Group Hamburg, I describe how I built a reusable Symfony2 Bundle that allows the user to add tags to any Entity without having to copy & paste source code.

1 Comment
3 Gefällt mir
Statistiken
Notizen
  • Wrong link on last slide.. Should be
    https://github.com/fogs/tagging-bundle
       Antworten 
    Sind Sie sicher, dass Sie...  Ja  Nein
    Ihre Nachricht erscheint hier
Keine Downloads
Views
Gesamtviews
1,608
Bei Slideshare
0
Aus Einbettungen
0
Anzahl an Einbettungen
0
Aktionen
Geteilt
0
Downloads
12
Kommentare
1
Gefällt mir
3
Einbettungen 0
No embeds

No notes for slide
  • Willkommen Fragen, Anmerkungen, Meinungen Vorstellung seit 2001 Software & Betrieb Fauler Entwickler Wiederverwendung Hürden Technologie Lizenz Umsetzung
  • Patterns Anti-Patterns Warum nicht? Robbenbaby
  • Text-Input? Pre-filled list / checkboxes Tags Suche bei Google, KnpBundles „Tag“ + „Symfony2“ ist vergiftet (DependencyInjection) Wo noch suchen?
  • Eindeutigkeit der Bezeichner
  • FormTypes als Service Erlaubt Injection vom TagManager
  • Code in Entity überflüssig machen Controller soll Entity nach Tags befragen Besser: Event werfen, wenn Entity dirty ist..
  • JS/CSS files sind nicht teil meines Projekts Composer kümmert sich um Aktualisierung
  • How I Built A Reusable Symfony2 TaggingBundle

    1. 1. Willkommen
    2. 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. 3. Scenario Community web application Users seek other users by interests How to input data?
    4. 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. 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. 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. 7. Missing Puzzle Pieces Form Widget Data Transformer Form Type
    8. 8. Form Widget Two input fields • visible for UI • hidden for „tagA, tagB, tagC“ Embed JavaScript & CSS Initialize JS
    9. 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. 10. Form Type Provide name for widget Called in Application FormTypes Gets TagManager injected Defines DataTransformer
    11. 11. Save the baby seals Entity Trait TaggableSubstriber make it reusable
    12. 12. Entity Trait avoid copy & paste for each functional Entity classes reference TagManager • after loading the Entity • to inform TagManager during flush
    13. 13. TaggableSubscriber postLoad() • attach TagManager to Entity postPersist() • TagManager->saveTagging($entity) onFlush() • save dirty Tags in TagManager Obsoletes code in Controllers
    14. 14. Make it reusable Dedicated Bundle Create composer project But how to include external JS & CSS?
    15. 15. Component RobLoach/component-installer Add package to composer.json Configure components-dir Run composer update See web/bundles
    16. 16. Live Demo
    17. 17. // TODO AppKernel: remove FPN bundle config.yml: remove FPN configuration Handling typeahead requests Configure tagmanager via Symfony Documentation :(
    18. 18. (re-)use it! http://github.com/fogs/TaggingBundle
    1. A particular slide catching your eye?

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

    ×