PHPCMS V9 採用模塊化架構,開發新模塊需遵循約定目錄結構。本文以 my_test 為例,完整梳理從建表到後台控制器的全過程。
phpcms/modules/my_test/
├── class/ # 業務邏輯類
├── function/ # 公共函數
├── install/ # 安裝 SQL、配置
├── language/ # 多語言包
├── templates/ # 模板文件 (*.tpl.php)
├── config.ini.php # 模塊元信息
├── extention.inc.php # 後台菜單、權限節點
├── model.php # 使用的模型列表
├── model.sql # 模型表記錄 SQL
├── my_test.sql # 業務表建表 SQL
├── uninstall/ # 卸載腳本
├── my_test.php # 後台控制器
└── index.php # 前台控制器(可選)
phpcms/model/my_test_model.class.php<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class my_test_model extends model {
public function __construct() {
$this->db_config = pc_base::load_config('database');
$this->db_setting = 'default';
$this->table_name = 'my_test'; // 無需前綴
parent::__construct();
}
}
繼承系統 model 類,自動獲得 insert/get_one/select/delete/update 等 CRUD 方法。
config.ini.php<?php
$module = 'my_test';
$modulename = '我的測試模塊';
$introduce = '演示模塊開發流程';
$author = 'Your Name';
$authorsite = 'https://example.com';
$authoremail = 'dev@example.com';
extention.inc.php<?php
// 插入菜單,返回菜單 ID
$menuId = $menu_db->insert([
'name' => 'my_test_list',
'parentid' => 0, // 頂級菜單或指定父 ID
'm' => 'my_test', // 模塊名
'c' => 'my_test', // 控制器
'a' => 'init', // 動作方法
'data' => '',
'listorder' => 1,
'display' => 1,
], true);
// 寫入語言包
$language = [
'my_test_list' => '列表管理',
'my_test_add' => '添加記錄',
];
model.php / model.sql// model.php
return ['my_test', 'my_test_article'];
// model.sql
INSERT INTO `phpcms_module` (`module`, `name`, `url`, `iscore`, `version`, `description`, `setting`, `listorder`, `disabled`, `installdate`, `updatedate`)
VALUES ('my_test', '我的測試模塊', '', 0, '1.0', '演示模塊', '', 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
my_test.sqlCREATE TABLE `phpcms_my_test` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(150) NOT NULL DEFAULT '',
`content` text,
`inputtime` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
my_test.php<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
class my_test extends admin {
public function __construct() {
parent::__construct();
}
public function init() {
$page = max(intval($_GET['page']), 1);
$data = $this->db->listinfo('', 'id DESC', $page, 20);
$pages = $this->db->pages;
include $this->admin_tpl('my_test_list');
}
public function add() {
if (isset($_POST['dosubmit'])) {
$this->db->insert($_POST['info']);
showmessage(L('operation_success'), HTTP_REFERER);
}
include $this->admin_tpl('my_test_add');
}
}
templates/my_test_add.tpl.php<form action="?m=my_test&c=my_test&a=add" method="post">
<table class="table_form">
<tr><th>標題</th><td><input name="info[title]" type="text" class="input-text"></td></tr>
<tr><th>內容</th><td><textarea name="info[content]" id="content"></textarea></td></tr>
</table>
<input type="submit" name="dosubmit" value="提交" class="dialog">
</form>
my_test → 提交。index.php?m=my_test&c=index&a=init(需自行編寫 index.php)。
pc_base::load_sys_class('model', '', 0) 第三參數 0 表示不實例化,僅加載類定義。{$var},邏輯用 {php ...},避免直接輸出未轉義內容。