Modular architecture to encapsulate functions in Symfony2 Modular architecture to encapsulate functions in Symfony2 symfony symfony

Modular architecture to encapsulate functions in Symfony2


I'd do it by hooking in to the EventDispatcher Component.

Your 'Master' Controller would define a set of events which correspond to the standard CRUD actions you've defined above ... onCreateNewElement(), onRenderEditorView(), etc. You'll probably find a dozen more to provide hooks for as you build your application (allowing plugins to add tools to a toolbar for example).

You'd define a Service (this doesn't HAVE to be a Controller Class), which looks for new 'modules' and adds their correctly-named methods as listeners for your custom application events.

Are modules = bundles? Well, that's entirely up to you. Are you going to provide a method for users to 'install' new modules specifically for your application? Then what a module needs to look like is entirely up to you. Are you going to slip Composer into the mix and allow modules to be installed that way? Places a couple of constraints on the structure, but still pretty much up to you.

How to realise the View-in-View? Again, that comes down to exactly how you define the interface for your 'Event Plugins' and what kind of access they have to resources like TWIG and YAML config.

The question you're REALLY asking is How do I add functionality to an application without hacking existing code ... and the answer is EventDispatcher. The rest is up to you.


There is another way to expand codebase with custom "modules": Dependency Injection Tags.

It works like this:

  • You create core of the app, which implements basic functionality and registers compiler pass which handles code extension
  • Modules can be part of the core app OR moved to external bundles
  • Each module must be registered in DI container with tag supported by your core app, so can be handled properly (adding menu positions, new element types as in your example)
  • Symfony DI compiles all services and stuff so all modules are available within app instantly (so you can for example iterate over collection of prepared items without need to dynamically building it)

In my opinion EventDispatcher is better for handling real-time actions, like sending notifications when new entity is created (e.g. controller/service dispatches event, some bundle subscribes it and send notification).

When you want to extend core I would recommend DI tags. Of course you have to build all the core (interfaces and whole architecture) so modules can extend existing services' data, then you only use your services.

Whole Symfony full-stack framework is based od this tags, so there is a lot of existing code to investigate for getting the concept.

Please let me know if it's clear enough what I wrote and what's in Symfony docs. If you need some additional info, I'll try to help.