Zend framework 2 article

Home >> Zend framework 2 >>

  Configure the module in Zend framework 2

This article will explain, how to configure the module in ZEND framework 2. In order to load and configure a module, Zend Framework 2 has a ModuleManager. This will look for Module.php in the root of the module directory (module/Blog) and expect to find a class called Blog\Module within it. That is, the classes within a given module will have the namespace of the module’s name, which is the directory name of the module.

Setting up the Blog module
Create Module.php in the Blog module: Create a file called Module.php under zf2app/module/Blog:

namespace Blog;

class Module

    public function getAutoloaderConfig()
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,

    public function getConfig()
        return include __DIR__ . '/config/module.config.php';


The ModuleManager will call getAutoloaderConfig() and getConfig() automatically for us.

Autoloading files
Our getAutoloaderConfig() method returns an array that is compatible with ZF2’s AutoloaderFactory. We configure it so that we add a class map file to the ClassMapAutoloader and also add this module’s namespace to the StandardAutoloader.

The standard autoloader requires a namespace and the path where to find the files for that namespace. It is PSR-0 compliant and so classes map directly to files as per the PSR-0 rules.

As we are in development, we don’t need to load files via the classmap, so we provide an empty array for the classmap autoloader. Create a file called autoload_classmap.php under zf2app/module/Blog

return array();

Note :
If you are using Composer, you could instead just create an empty getAutoloaderConfig() { } and add to composer.json

"autoload": {
     "psr-0": { "Blog": "module/Blog/src/" }

If you go this way, then you need to update the composer autoloading files.

Having registered the autoloader, let’s have a quick look at the getConfig() method in Blog\Module. This method simply loads the config/module.config.php file.

Create a file called module.config.php under zf2app/module/Blog/config:

return array(
    'controllers' => array(
        'invokables' => array(
            'Blog\Controller\Blog' => 'Blog\Controller\BlogController',

    'view_manager' => array(
        'template_path_stack' => array(
            'stickynotes' => __DIR__ . '/../view',

The config information is passed to the relevant components by the ServiceManager. We need two initial sections: controllers and view_manager

Informing the application about our new module
We now need to tell the ModuleManager that this new module exists. This is done in the application’s config/application.config.php

return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Blog' // Add this line to activate blog module.

As you can see, we have added our Blog module into the list of modules after the Application module.

Write your comment now