萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHPCMS V9 模塊開發全流程指南:目錄結構、模型、控制器、安裝入口

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           # 前台控制器(可選)

核心文件詳解

1. 數據模型 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 方法。

2. 模塊元信息 config.ini.php

<?php
$module = 'my_test';
$modulename = '我的測試模塊';
$introduce = '演示模塊開發流程';
$author = 'Your Name';
$authorsite = 'https://example.com';
$authoremail = 'dev@example.com';

3. 後台菜單與權限 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'  => '添加記錄',
];

4. 模型聲明 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());

5. 建表 SQL my_test.sql

CREATE 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;

6. 後台控制器 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');
    }
}

7. 模板文件 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>

安裝驗證

  1. 後台 → 模塊管理 → 安裝模塊 → 選擇 my_test → 提交。
  2. 2. 左側菜單出現「列表管理」,點擊進入列表/添加頁面。 3. 前台可訪問 index.php?m=my_test&c=index&a=init(需自行編寫 index.php)。

避坑提示

  • 所有文件編碼必須為 UTF-8 無 BOM。
  • 類名、文件名區分大小寫,Linux 區分大小寫。
  • pc_base::load_sys_class('model', '', 0) 第三參數 0 表示不實例化,僅加載類定義。
  • 模板變量輸出用 {$var},邏輯用 {php ...},避免直接輸出未轉義內容。
copyright © 萬盛學電腦網 all rights reserved