Question

How can I manipulate other modules without editing them ? very the same thing that wordpress modules do . They add functionality to core system without changing the core code and they work together like a charm.

I always wanted to know how to implement this in my own modular application

Was it helpful?

Solution 2

This is a long topic, but here is a short gist.

Extensibility in Zend Framework 2 heavily relies on the premise that components can be interchanged, added, and/or substituted.

Read up on SOLID principles: http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

Modules typically consists of objects working together as a well-oiled machinery, designed to accomplish one thing or a bunch of related things, whatever that may be. These objects are called services, and managed by the service locator/service manager.

A big part of making your module truly extensible is to expect your developers to extend a class or implement a certain interface, which the developer register as services. You should provide a mode of definition wherein the developers can specify which things he wants to substitute, and/or add their own services to -- and this is where the application configuration comes in.

Given the application configuration, you should construct your machinery a.k.a. module services according to options the developer has specified i.e., use the developer defined Foo\Bar\UserService service as the YourModule\UserServiceInterface within your module, etc. (This is usually delegated to service factories, which has the opportunity to read the application configuration, and constructs the appropriate object given a particular set of configuration values.)

EDIT:

To add, a lot can be accomplished by leveraging Zend's Zend\EventManager component. This allows you to give developers the freedom to hook and listen to certain operations of your module and act accordingly (See: http://en.wikipedia.org/wiki/Observer_pattern)

OTHER TIPS

A long time ago I wrote the blog post "Use 3rd party modules in Zend Framework 2" specifically about extending Zend Framework 2 modules. The answer from Bez is technically correct, it could be a bit more specific about the framework.

Read the full post at https://juriansluiman.nl/article/117/use-3rd-party-modules-in-zend-framework-2, but it gives you a clue about:

  1. Changing a route from a module (say, you want to have the url /account/login instead of /user/login)
  2. Overriding a view script, so you can completely modify the page's rendering
  3. Changing a form object, so you could add new form fields or mark some required field as not required anymore.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top