在本章中,我们将学习如何在Zend Framework中创建一个基于MVC的模块。让我们创建一个名为Tutorial的模块来了解模块创建过程。
在-myapp / module / Tutorial / src /目录下创建名为Module的新的PHP类,并实现ConfigProviderInterface。
将教程设置为Module类的命名空间。
在Module类中编写一个public functiongetConfig,并返回TutorialModule的配置文件。
Module类的完整代码如下:
配置教程在模块composer.json下自动加载通过使用以下代码段。
"autoload":{"psr-4":{"Application":"module/Application/src/","Tutorial":"module/Tutorial/src/"}}
使用composerupdate命令更新应用程序,如下所示。
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”
<?phpnamespaceTutorial ;useZendServiceManagerFactoryInvokableFactory;useZendRouterHttpSegment;return["controllers"=>["factories"=>[ControllerTutorialController::class=>InvokableFactory::class,],],"view_manager"=>["template_path_stack"=>["tutorial"=>__DIR__."/../view",],],];
配置文件有三个部分,如下所示:
控制器配置- 指定模块内可用的控制器。
路由配置- 指定如何将模块中的控制器解析为URL。
查看配置- 指定查看引擎的相关配置,如视图位置等。
配置教程应用程序级配置文件中的模块- MYAPP /配置/ modules.config.php。
return ["ZendRouter", "ZendValidator", "Application", "Tutorial"];
通过执行应用程序文件夹根目录下的作曲者投放来运行应用程序。
We have successfully added a new module, but we still need to add the Controller, Routing and Views to successfully run the Tutorial module.