Zend框架-创建模块

在本章中,我们将学习如何在Zend Framework中创建基于MVC的模块。让我们创建一个称为Tutorial的模块,以了解模块的创建过程。

  • 在–myapp / module / Tutorial / src /目录中创建一个名为Module的新PHP类,并实现ConfigProviderInterface。
  • Tutorial设置为Module类的名称空间。
  • Module类中编写一个公共函数getConfig,然后返回Tutorial Module的配置文件。

Module类的完整代码如下-

<?php  
namespace Tutorial; 
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface { 
   public function getConfig() {    
      return include __DIR__ . '/../config/module.config.php'; 
   }    
} 

使用以下代码在composer.json中autoload部分下配置Tutorial模块。

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

如下所示,使用composer update命令更新应用程序。

composer update 

作曲家命令将执行到应用程序必要的改变,并显示在命令提示日志如下所示-

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 

使用以下代码在/ config /创建模块配置文件“ module.config.php” –

<?php  
namespace Tutorial;  
   
use Zend\ServiceManager\Factory\InvokableFactory; 
use Zend\Router\Http\Segment;  
return [ 
   'controllers' => [ 
      'factories' => [Controller\TutorialController::class => InvokableFactory::class,], 
   ],
   'view_manager' => [ 
      'template_path_stack' => ['tutorial' => __DIR__ . '/../view',], 
   ], 
];

配置文件分为三部分,分别如下:

  • 控制器配置-指定模块内可用的控制器。
  • 路由配置-指定应如何将模块中的控制器解析为URL。
  • 视图配置-指定与视图引擎相关的配置,例如视图的位置等。

在应用程序级别配置文件myapp / config / modules.config.php中配置Tutorial模块。

return ['Zend\Router', 'Zend\Validator', 'Application', 'Tutorial'];

通过执行位于应用程序文件夹根目录的composer服务来运行应用程序。

我们已经成功添加了一个新模块,但是仍然需要添加Controller,RoutingViews才能成功运行Tutorial模块。

作者:terry,如若转载,请注明出处:https://www.web176.com/zendframework/300.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年10月23日 下午1:58
下一篇 2020年10月23日 下午2:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注