How to create module in Zend Framework 3

What is module in Zend framework?
Module is resuabale software component that can be used to create a complete application. So, for example, you can write a module for user authentication and you can use it over and over again. In other worrd we can say built it once and use it many times

You can write your module in a single folder and you can reuse it in your other Zend applications you have

First, you can create the Zend skeleton application

How to create the module
There are two ways which can be used to create the new module in your application
1. You can copy and paste the existing module and rename the module

You can see the Application module in your skeleton application. You can simply copy and paste application folder and rename it to User so that you can create the User module. Finally you can remove the unwanted files in User folder and add the new code according to your requirement

2 Download the skeleton module from the server
The second way of creating the module is, downloading the skeleton module from here . You can copy unzipped folder to the module folder in your Zend skeleton application

You can see the folder structure of the skeleton module

You can change the name of the module to “User”

Enabling Class Autoloading
Now you can enable auotoloading for the module “User”
You can open the composer.json file in the root of your project and add the following code

Next you can run the dump-autoload commad in the terminal

So this will update the file located at vendor\composer\autoload_psr4.php . You can see the User entry in the file

Enable and load the module
You need to enable and let the ZF know about the module and load the module when the app start up. So you can add the module name to the config\modules.config.php file

Now you have done basic configuration for the module. We will create a new contrller and define the rote to test the module

I am going to create UserController.php at module\User\src\Controller and add the following code

Please make sure you have the correct namespace in the code

I have modified the code in module\User\config\module.config.php to make routing work. This will enable you to open the webpage with http://localhost:8000/user

You can add the following code to load the UserController

You have to change the namespace of this file and other files to make the app works

Now lets create the view file index.phtml at module\User\view\user\user and add the following code

When you open the http://localhost:8000/user in your browser you can see the following output

  • Iranga Niroshan

    This is great tutorial to getting started with module development in zend framework

  • Mostafa Elsayh

    $serviceManager = new ServiceManager([
    ‘abstract_factories’ => [
    new MyAbstractFactory() // You could also pass a class name: MyAbstractFactory::class

    how do you use it?

  • Ronak

    hey buddy,

    I am getting error here

    Terminal/Documents$ php -S -t public/ public/index.php

    PHP Warning: PHP Startup: Unable to load dynamic library ‘’ (tried: /usr/lib/php/20180731/ (/usr/lib/php/20180731/ cannot open shared object file: No such file or directory), /usr/lib/php/20180731/ (/usr/lib/php/20180731/ cannot open shared object file: No such file or directory)) in Unknown on line 0
    Directory public/ does not exist.

    please help me.. ?

  • Ronak

    Hey buddy,

    getting Error 2

    /Documents/zen/path/to/install$ php -S -t public public/index.php
    PHP Development Server started at Thu Apr 25 17:15:10 2019
    Listening on

    Stack trace:
    #0 /home/Documents/zen/path/to/install/vendor/zendframework/zend-modulemanager/src/ModuleManager.php(175): ZendModuleManagerModuleManager->loadModuleByName(Object(ZendModuleManagerModuleEvent))

    #1 /home/Documents/zen/path/to/install/vendor/zendframework/zend-modulemanager/src/ModuleManager.php(97): ZendModuleManagerModuleManager->loadModule(‘User’)

    #2 /home/Documents/zen/path/to/install/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): ZendModuleManagerModuleManager->onLoadModules(Object(ZendModuleManagerModuleEvent))

    #3 /home/Documents/zen/path/to/install/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): ZendEventManagerEventManager->triggerListeners(Object(ZendModuleManagerModuleEvent))

    #4 /home/ in /home/Documents/zen/path/to/install/vendor/zendframework/zend-modulemanager/src/ModuleManager.php on line 203