How to create module in Zend Framework 3

Last Updated: January 31, 2018

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

"autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/",
            "User\\": "module/User/src/"

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

composer dump-autoload

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

return [

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

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class UserController extends AbstractActionController
    public function indexAction()
    	echo "Hello User";
        return null;

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

'router' => [
        'routes' => [
            'user' => [
                'type'    => 'Literal',
                'options' => [
                    // Change this to something specific to your module
                    'route'    => '/user',
                    'defaults' => [
                        'controller'    => Controller\UserController::class,
                        'action'        => 'index',
                'may_terminate' => true,
                'child_routes' => [
                    // You can place additional routes that match under the
                    // route defined above here.

You can add the following code to load the UserController

'controllers' => [
        'factories' => [
            Controller\UserController::class => InvokableFactory::class,

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

Module:        User »
Controller:    UserController »
Action:        index

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