A brief tour of the highlights of the Zend Framework

Is it just a framework, or does this framework epitomize the pride of the PHP community — its hard-working developers, the key ingredient, so to speak? With a scattering of configs ... The object of love of our PL, with a good MVC, thanks to which Zend Framework is the best PHP framework.

Here you will not find the answer to this question, but you will learn about the ServiceManager and ModuleManager.

Run fools!

A warning

  1. This material is based on what I was looking for on the Zend Framework 2, in some places even mentions in version 1 appear. *. I do not think that this will become a problem when compared with other versions, since fundamental points are considered and they are unlikely to change globally.
  2. In the arguments and translations (as well as in paragraph 1) there may be blunders, both mine and original sources. All will be given links, and their own creativity will be with a note [mine] .
  3. Focused on those who googled and, like me, confused. There will be no deployment of blogs, explanations and interactive. But there will be two pictures and a kitty.


  • Terms
  • Framework device
    • General structure and connections
    • Plugins
  • Visualization
    • Connections
    • Schematic representation
  • From the author
    • useful links
  • Bonus


Source , a little [mine] .

  • Application - the final product site;
  • Module - a functionally complete "block" of an application, whose code may consist of models, views, controllers. The module extends the functionality of a web application and can function only "inside" it; Modules are registered in application.config.phpthe section.modules
  • ModuleManager - a container for manipulating modules;
  • Service - "mechanism" in the module, for manipulations between models, controllers, types, etc.; Services are registered in module.config.phpthe section service_manager.
  • ServiceManager - a container for service manipulations.
  • ControllerManager - works with services and factories for loading controllers ( \Zend\ServiceManager\AbstractFactoryInterfaceor \Zend\ServiceManager\ServiceManager). dock
  • EventManager is a component that aggregates event handlers (Listener) for one or more named events (Event), and also initiates the processing of these events.
  • A plugin is a class that in some way extends the functionality of all controllers.

Framework device

General structure and connections

A source

When created Zend\Mvc\Application, an object Zend\ServiceManager\ServiceManageris created and configured via Zend\Mvc\Service\ServiceManagerConfig. ServiceManagerConfiggets the configuration from config/application.config.php(or some other config of the application, which is passed in Applicationwhen it is created). Of all the services and factories represented in the namespace Zend\Mvc\Service, ServiceManagerConfigonly three are responsible: SharedEventManager,, EventManagerand ModuleManager.

After that Applicationretrieves ModuleManager. At this moment ModuleManagerthrough ServiceManagerconfigures the services and factories provided in Zend\Mvc\Service\ServiceListenerFactory. This approach makes it possible to simplify the configuration of the main application as much as possible and to provide the developer with the ability to configure various parts of the MVC system from modules, overriding any default configuration in the services of these MVCs.

ModuleManager, Expressed in Zend\Mvc\Service\ModuleManagerFactory. This is probably the most complex factory in the MVC stack. ModuleManagerexpects the service to be ApplicationConfigimplemented ( Di ) with keys module_listener_optionsand modules.

It creates an instance Zend\ModuleManager\Listener\DefaultListenerAggregateusing the extracted module_listener_options. It then checks if the service exists with the name ServiceListener; if not, it uses the factory with the name Zend\Mvc\Service\ServiceListenerFactory. As ServiceListenerwill be added to multiple services listeners, such as methods of listeners getServiceConfig, getControllerConfig, getControllerPluginConfig, getViewHelperConfigmodule.

Then ModuleManagerretrieves the service EventManagerand attaches the aforementioned listeners. It creates an instance Zend\ModuleManager\ModuleEventby setting the "ServiceManager" parameter in the service manager object. Finally, it instantiates Zend\ModuleManager\ModuleManagerand injects EventManagerand ModuleEvent.

[my] The case when the code is clearer:

    publicfunctioncreateService(ServiceLocatorInterface $serviceLocator){
        if (!$serviceLocator->has('ServiceListener')) {
            $serviceLocator->setFactory('ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory');
        $configuration    = $serviceLocator->get('ApplicationConfig');
        $listenerOptions  = new ListenerOptions($configuration['module_listener_options']);
        $defaultListeners = new DefaultListenerAggregate($listenerOptions);
        $serviceListener  = $serviceLocator->get('ServiceListener');
        );  // то же самое для остальных методов модуля
        $events = $serviceLocator->get('EventManager');
        $moduleEvent = new ModuleEvent;
        $moduleEvent->setParam('ServiceManager', $serviceLocator);
        $moduleManager = new ModuleManager($configuration['modules'], $events);
        return $moduleManager;


A source

The controller architecture includes a plugin system that allows you to add your own code, which will be called when certain events occur during the life of the controller. The front controller uses the plugin broker as a registry of user plug-ins, the plug-in broker also provides a call to event methods in each plugin registered through the front controller.

Event methods are defined in an abstract class Zend_Controller_Plugin_Abstractfrom which all user plugins should inherit.



  • Reads from top to bottom, unless otherwise specified by arrows.
  • Lines with arrows indicate what is included.
  • Thin lines without arrows indicate what is connected with what.
  • Thick lines with no arrows indicate what controls.





From the author

The attentive reader noted that the article begins with a link to the Toaster, where a question is asked about the differences between ServiceManager and ModuleManager, and the text of the article begins with them. Coincidence? I do not think. The fact is that Habr became the first place where I started to get acquainted with the basics of the framework and confusion was introduced by the publication where the blog was recreated from the documentation with comments from the author of the article. It was the lack of a description of the ModuleManager that pushed me to the wrong reasoning (that modules are registered in the ServiceManager) and this led to the writing of this article.

useful links

I do not want to engage in copy-paste and grow 6 parts per subject, so I attach a list of my bookmarks on ZF with notes:

Beware, spoiler!


In three articles c Habra

  • https://habr.com/post/192522/
  • Вольный перевод документации по разработке простого блога на ZendSkeletonApplication. Рассказывается о конфигах, ServiceManager (или ModuleManager, я так и не понял) и подключении сторонних библиотек.

Оригинал документации на блог


Первичный обзор

Подробный разбор


Quick start

Подробный разбор



I hope the brief excursion didn’t take too long and was useful. Of course, edits, suggestions, criticisms and other actions permitted by the Habr rules and the current legislation of the Russian Federation are accepted on your part.

The bonus we deserve:


(= ^ ・ Ω ・ ^ =)

Also popular now: