- Zend Framework 教程
- Zend Framework - 首頁
- Zend Framework - 簡介
- Zend Framework - 安裝
- 框架應用
- Zend Framework - MVC 架構
- Zend Framework - 概念
- Zend Framework - 服務管理器
- Zend Framework - 事件管理器
- Zend Framework - 模組系統
- 應用結構
- Zend Framework - 建立模組
- Zend Framework - 控制器
- Zend Framework - 路由
- Zend Framework - 檢視層
- Zend Framework - 佈局
- 模型與資料庫
- 不同的資料庫
- 表單與驗證
- Zend Framework - 檔案上傳
- Zend Framework - Ajax
- Cookie 管理
- 會話管理
- Zend Framework - 身份驗證
- 郵件管理
- Zend Framework - 單元測試
- Zend Framework - 錯誤處理
- Zend Framework - 工作示例
- Zend Framework 有用資源
- Zend Framework - 快速指南
- Zend Framework - 有用資源
- Zend Framework - 討論
Zend Framework - 建立模組
本章我們將學習如何在 Zend Framework 中建立一個基於 MVC 的模組。讓我們建立一個名為Tutorial的模組來理解模組建立過程。
在 –myapp/module/Tutorial/src/ 目錄下建立一個名為Module的新 PHP 類,並實現 ConfigProviderInterface 介面。
將Tutorial設定為Module類的名稱空間。
在Module類中編寫一個公共函式getConfig,並返回Tutorial模組的配置檔案。
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
composer命令將對應用程式進行必要的更改,並在命令提示符中顯示日誌,如下所示:
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 serve執行應用程式。
我們已經成功添加了一個新模組,但是為了成功執行Tutorial模組,我們仍然需要新增控制器、路由和檢視。
廣告