Zend Framework – Creating a Module
”;
In this chapter, we will learn how to create a MVC based module in the Zend Framework. Let us create a module called as Tutorial to understand the module creation process.
-
Create a new PHP class named Module inside the –myapp/module/Tutorial/src/ directory and implement the ConfigProviderInterface.
-
Set Tutorial as the namespace for the Module class.
-
Write a public function getConfig in the Module class and return the configuration file for the Tutorial Module.
The complete code for the Module class is as follows −
<?php namespace Tutorial; use ZendModuleManagerFeatureConfigProviderInterface; class Module implements ConfigProviderInterface { public function getConfig() { return include __DIR__ . ''/../config/module.config.php''; } }
Configure the Tutorial module in the composer.json under the autoload section by using the following code.
"autoload": { "psr-4": { "Application\": "module/Application/src/", "Tutorial\": "module/Tutorial/src/" } }
Update the application using the composer update command as shown below.
composer update
The composer command will do necessary change to the application and show the logs in the command prompt as shown below −
Loading composer repositories with package information Updating dependencies (including require-dev) - Removing zendframework/zend-component-installer (0.3.0) - Installing zendframework/zend-component-installer (0.3.1) Downloading: 100% - Removing zendframework/zend-stdlib (3.0.1) - Installing zendframework/zend-stdlib (3.1.0) Loading from cache - Removing zendframework/zend-eventmanager (3.0.1) - Installing zendframework/zend-eventmanager (3.1.0) Downloading: 100% - Removing zendframework/zend-view (2.8.0) - Installing zendframework/zend-view (2.8.1) Loading from cache - Removing zendframework/zend-servicemanager (3.1.0) - Installing zendframework/zend-servicemanager (3.2.0) Downloading: 100% - Removing zendframework/zend-escaper (2.5.1) - Installing zendframework/zend-escaper (2.5.2) Loading from cache - Removing zendframework/zend-http (2.5.4) - Installing zendframework/zend-http (2.5.5) Loading from cache - Removing zendframework/zend-mvc (3.0.1) - Installing zendframework/zend-mvc (3.0.4) Downloading: 100% - Removing phpunit/phpunit (5.7.4) - Installing phpunit/phpunit (5.7.5) Downloading: 100% Writing lock file Generating autoload files
Create the module configuration file, “module.config.php” at /config/ with the following code −
<?php namespace Tutorial; use ZendServiceManagerFactoryInvokableFactory; use ZendRouterHttpSegment; return [ ''controllers'' => [ ''factories'' => [ControllerTutorialController::class => InvokableFactory::class,], ], ''view_manager'' => [ ''template_path_stack'' => [''tutorial'' => __DIR__ . ''/../view'',], ], ];
The configuration file has three parts and they are as follows −
-
Controller configuration − Specify the controllers available inside the Module.
-
Routing configuration − Specify how the controllers in the module should be resolved into URLs.
-
View configuration − Specify the configuration related to view the engine such as the location of views, etc.
Configure the Tutorial module in the application level configuration file – myapp/config/modules.config.php.
return [''ZendRouter'', ''ZendValidator'', ''Application'', ''Tutorial''];
Run the application by executing the composer serve at the root of the application folder.
We have successfully added a new module, but we still need to add the Controller, Routing and Views to successfully run the Tutorial module.
”;