Creating a Module in ZF2

This article will focus on creating a module in ZF2. If you have read the previous article about the introduction to the ZF2 Skeleton Application, you should be well equipped to start creating your own module and building on top of the framework.

To start with, a name for the module is needed. For the purpose of this article, the module will be called ‘Siphor’. So we need a to create a ‘Siphor’ directory within the modules/ directory.

The basic requirements of a module is to have a Module.php class file, so go ahead and create that now.

// module/Siphor/Module.php
<?php
namespace Siphor;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

The Module.php file will be responsible for loading the module-specific configuration file, module.config.php and the StandardAutoloader class that will be used to autoload our source files located in the src/ directory.

Now under config/application.config.php, add the name of the module within the modules array to register the module with the application.

// config/application.config.php
<?php
return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Application',
        'Siphor',
    ),
    ....
);

As the getConfig() method of the Module.php class suggests, the module-specific configuration will be located under the config/ directory.

Add a module.config.php file and add a route so that we can access the custom module.

// module/Siphor/config/module.config.php
<?php
return array(
    'router' => array(
        'routes' => array(
            'siphor' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/siphor',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Siphor\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Siphor\Controller\Index' => 'Siphor\Controller\IndexController'
        ),
    ),
);

The controller class should be added under the ‘invokables’ key within the ‘controllers’ key. This lets the application know about the controller class added.

Basic controllers will only need to extend from Zend\Mvc\Controller\AbstractActionController

// module/Siphor/src/Siphor/Controller/IndexController.php
<?php
namespace Siphor\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        echo "Hello World!";
        exit();
    }
}

Adding “/siphor” onto the end of your ZF2 application root URL, you should now see the following.

Creating a Module in ZF2

View models are responsible for rendering templates, and therefore content in the web browser.

Replacing the contents of the indexAction() method with the following:

// module/Siphor/src/Siphor/Controller/IndexController.php
<?php
namespace Siphor\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

This error message will occur:

Creating a Module in ZF2

As per the name of the module, controller and action, the ViewModel is expecting to render the ‘siphor/index/index’ template.

To add a template file first of all we need to specify the location of the templates in module.config.php.

// module/Siphor/config/module.config.php
<?php
return array(
    ....
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    )
);

This tells the application that our template files will be located under:

module/Siphor/view/

Now to add the template file, we need to add it in the following location.

module/Siphor/view/siphor/index/index.phtml

For simplicity, the view file will just contain some dummy text.

// module/Siphor/view/siphor/index/index.phtml
<p>Here is some content!</p>

When refreshing the web page, you should be presented with a working page plus the content in the view file being rendered.

Creating a Module in ZF2

Note: This article is based on ZF2 version 2.4.