Cấu trúc mô-đun
Tổng quan
Phần tiêu đề “Tổng quan”Cấu trúc mô-đun được tổ chức tốt là nền tảng để phát triển XOOPS có thể duy trì. Hướng dẫn này bao gồm cả bố cục mô-đun cũ và hiện đại (PSR-4).
Bố cục mô-đun tiêu chuẩn
Phần tiêu đề “Bố cục mô-đun tiêu chuẩn”Cấu trúc kế thừa
Phần tiêu đề “Cấu trúc kế thừa”modules/mymodule/├── admin/ # Admin panel files│ ├── index.php # Admin dashboard│ ├── menu.php # Admin menu definition│ ├── permissions.php # Permission management│ └── templates/ # Admin templates├── assets/ # Frontend resources│ ├── css/│ ├── js/│ └── images/├── class/ # PHP classes│ ├── Common/ # Shared utilities│ │ ├── Breadcrumb.php│ │ └── Configurator.php│ ├── Form/ # Custom form elements│ └── Handler/ # Object handlers├── include/ # Include files│ ├── common.php # Common initialization│ ├── functions.php # Utility functions│ ├── oninstall.php # Installation hooks│ ├── onupdate.php # Update hooks│ └── onuninstall.php # Uninstallation hooks├── language/ # Translations│ ├── english/│ │ ├── admin.php # Admin strings│ │ ├── main.php # Frontend strings│ │ ├── modinfo.php # Module info strings│ │ └── help/ # Help files│ └── other_language/├── sql/ # Database schemas│ └── mysql.sql # MySQL schema├── templates/ # Smarty templates│ ├── admin/│ └── blocks/├── blocks/ # Block functions├── preloads/ # Preload classes├── xoops_version.php # Module manifest├── header.php # Module header├── footer.php # Module footer└── index.php # Main entry pointCấu trúc PSR-4 hiện đại
Phần tiêu đề “Cấu trúc PSR-4 hiện đại”modules/mymodule/├── src/ # PSR-4 autoloaded source│ ├── Controller/ # Request handlers│ │ ├── ArticleController.php│ │ └── CategoryController.php│ ├── Service/ # Business logic│ │ ├── ArticleService.php│ │ └── CategoryService.php│ ├── Repository/ # Data access│ │ ├── ArticleRepository.php│ │ └── ArticleRepositoryInterface.php│ ├── Entity/ # Domain objects│ │ ├── Article.php│ │ └── Category.php│ ├── DTO/ # Data transfer objects│ │ ├── CreateArticleDTO.php│ │ └── UpdateArticleDTO.php│ ├── Event/ # Domain events│ │ └── ArticleCreatedEvent.php│ ├── Exception/ # Custom exceptions│ │ └── ArticleNotFoundException.php│ ├── ValueObject/ # Value types│ │ └── ArticleId.php│ └── Middleware/ # HTTP middleware│ └── AuthenticationMiddleware.php├── config/ # Configuration│ ├── routes.php # Route definitions│ ├── services.php # DI container config│ └── events.php # Event listeners├── migrations/ # Database migrations│ ├── 001_create_articles.php│ └── 002_add_indexes.php├── tests/ # Test files│ ├── Unit/│ └── Integration/├── templates/ # Smarty templates├── language/ # Translations (JSON)│ ├── en/│ │ └── main.json│ └── de/├── assets/ # Frontend resources├── module.json # Module manifest (XOOPS 4.0)└── composer.json # Composer configGiải thích các tập tin chính
Phần tiêu đề “Giải thích các tập tin chính”xoops_version.php (Bản kê khai kế thừa)
Phần tiêu đề “xoops_version.php (Bản kê khai kế thừa)”<?php$modversion = [ 'name' => 'My Module', 'version' => '1.0.0', 'description' => 'Module description', 'author' => 'Your Name', 'credits' => 'Contributors', 'license' => 'GPL 2.0', 'dirname' => basename(__DIR__), 'modicons16' => 'assets/images/icons/16', 'modicons32' => 'assets/images/icons/32', 'image' => 'assets/images/logo.png',
// System 'system_menu' => 1, 'hasAdmin' => 1, 'adminindex' => 'admin/index.php', 'adminmenu' => 'admin/menu.php', 'hasMain' => 1,
// Database 'sqlfile' => ['mysql' => 'sql/mysql.sql'], 'tables' => ['mymodule_items', 'mymodule_categories'],
// Templates 'templates' => [ ['file' => 'mymodule_index.tpl', 'description' => 'Index page'], ['file' => 'mymodule_item.tpl', 'description' => 'Item detail'], ],
// Blocks 'blocks' => [ [ 'file' => 'blocks/recent.php', 'name' => '_MI_MYMOD_BLOCK_RECENT', 'description' => '_MI_MYMOD_BLOCK_RECENT_DESC', 'show_func' => 'mymodule_recent_show', 'edit_func' => 'mymodule_recent_edit', 'template' => 'mymodule_block_recent.tpl', 'options' => '5|0', ], ],
// Config 'config' => [ [ 'name' => 'items_per_page', 'title' => '_MI_MYMOD_ITEMS_PER_PAGE', 'description' => '_MI_MYMOD_ITEMS_PER_PAGE_DESC', 'formtype' => 'textbox', 'valuetype' => 'int', 'default' => 10, ], ],];module.json (Bản kê khai XOOPS 4.0)
Phần tiêu đề “module.json (Bản kê khai XOOPS 4.0)”{ "name": "My Module", "slug": "mymodule", "version": "1.0.0", "description": "Module description", "author": "Your Name", "license": "GPL-2.0-or-later", "php": ">=8.2",
"namespace": "XoopsModules\\MyModule", "autoload": "src/",
"admin": { "menu": "config/admin-menu.php" },
"routes": "config/routes.php", "services": "config/services.php", "events": "config/events.php",
"templates": [ {"file": "index.tpl", "description": "Index page"} ],
"config": { "items_per_page": { "type": "int", "default": 10, "title": "@mymodule.config.items_per_page" } }}Mục đích của thư mục
Phần tiêu đề “Mục đích của thư mục”| Thư mục | Mục đích |
|---|---|
admin/ | Giao diện quản trị |
assets/ | CSS, JavaScript, hình ảnh |
blocks/ | Khối chức năng kết xuất |
class/ | PHP classes (cũ) |
config/ | Tệp cấu hình (hiện đại) |
include/ | Tệp include được chia sẻ |
language/ | Tập tin dịch |
migrations/ | Di chuyển cơ sở dữ liệu |
sql/ | Lược đồ cơ sở dữ liệu ban đầu |
src/ | Mã nguồn PSR-4 |
templates/ | Smarty templates |
tests/ | Tập tin thử nghiệm |
Các phương pháp hay nhất
Phần tiêu đề “Các phương pháp hay nhất”- Các mối quan tâm riêng biệt - Loại bỏ logic kinh doanh khỏi templates
- Sử dụng không gian tên - Sắp xếp mã với không gian tên thích hợp
- Tuân theo PSR-4 - Sử dụng các quy ước tự động tải tiêu chuẩn
- Externalize Config - Giữ cấu hình tách biệt với mã
- Cấu trúc tài liệu - Bao gồm tổ chức giải thích README
Tài liệu liên quan
Phần tiêu đề “Tài liệu liên quan”- Phát triển mô-đun - Hướng dẫn phát triển hoàn chỉnh
- Các phương pháp thực hành tốt nhất/Tổ chức theo mã - Các mẫu tổ chức mã
- Bản kê khai mô-đun - Cấu hình bản kê khai
- Cơ sở dữ liệu/Lược đồ cơ sở dữ liệu - Thiết kế cơ sở dữ liệu