English 中文(简体)
Zend Framework - Creating Module
  • 时间:2024-09-17

Zend Framework - Creating a Module


Previous Page Next Page  

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 pubpc 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 { 
   pubpc 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": { 
      "Apppcation\": "module/Apppcation/src/", 
      "Tutorial\": "module/Tutorial/src/" 
   } 
}

Update the apppcation using the composer update command as shown below.

composer update 

The composer command will do necessary change to the apppcation 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) 
   - Instalpng zendframework/zend-component-installer (0.3.1) 
   Downloading: 100%           
   
   - Removing zendframework/zend-stdpb (3.0.1) 
   - Instalpng zendframework/zend-stdpb (3.1.0) 
   Loading from cache  
   
   - Removing zendframework/zend-eventmanager (3.0.1) 
   - Instalpng zendframework/zend-eventmanager (3.1.0) 
   Downloading: 100%           
   
   - Removing zendframework/zend-view (2.8.0) 
   - Instalpng zendframework/zend-view (2.8.1) 
   Loading from cache  
   
   - Removing zendframework/zend-servicemanager (3.1.0) 
   - Instalpng zendframework/zend-servicemanager (3.2.0) 
   Downloading: 100%           
   
   - Removing zendframework/zend-escaper (2.5.1) 
   - Instalpng zendframework/zend-escaper (2.5.2) 
   Loading from cache  
   
   - Removing zendframework/zend-http (2.5.4) 
   - Instalpng zendframework/zend-http (2.5.5) 
   Loading from cache  
   
   - Removing zendframework/zend-mvc (3.0.1) 
   - Instalpng zendframework/zend-mvc (3.0.4) 
   Downloading: 100%          
   
   - Removing phpunit/phpunit (5.7.4) 
   - Instalpng 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 apppcation level configuration file – myapp/config/modules.config.php.

return [ ZendRouter ,  ZendVapdator ,  Apppcation ,  Tutorial ];

Run the apppcation by executing the composer serve at the root of the apppcation 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.

Advertisements