setup.php
前先認識一下前台頁面結構,共有五大部份:
use
設定,端看下方用了哪些物件方法隨時加入
<?php
use Xmf\Request;
use XoopsModules\Submission\Submission;
use XoopsModules\Tadtools\TadUpFiles;
use XoopsModules\Tadtools\Utility;
/*-----------引入檔案區--------------*/
require_once __DIR__ . '/header.php';
$GLOBALS['xoopsOption']['template_main'] = 'submission_setup.tpl';
require_once XOOPS_ROOT_PATH . '/header.php';
header.php
是模組自己的 header.php
,主要用來引入XOOPS最重要的設定檔 maindile.php
以及剛剛的 interface.php
檔案。templates
目錄下,必須在引入系統的 header.php
檔前設定好。header.php
檔,用來載入產生網站畫面用的資訊,以及載入區塊等各種重要功能。
/*-----------變數過濾----------*/
$op = Request::getString('op');
$id = Request::getInt('id');
$files_sn = Request::getInt('files_sn');
$op
用來作為流程判斷依據,幾乎每個模組都是這樣用。Request
是XOOPS的過濾物件,上方記得加入 use Xmf\Request;
$op
來判斷接下來要做什麼事:
/*-----------執行動作判斷區----------*/
switch ($op) {
//新增資料
case 'submission_store':
$id = Submission::store();
header("location: {$_SERVER['PHP_SELF']}?id=$id");
exit;
//更新資料
case 'submission_update':
$where_arr['id'] = $id;
Submission::update($where_arr);
header("location: {$_SERVER['PHP_SELF']}?id=$id");
exit;
//下載檔案
case 'tufdl':
$TadUpFiles = new TadUpFiles("submission");
$TadUpFiles->add_file_counter($files_sn, false);
exit;
//新增用表單
case 'submission_create':
Submission::create();
break;
//修改用表單
case 'submission_edit':
Submission::create($id);
$op = 'submission_create';
break;
//刪除資料
case 'submission_destroy':
Submission::destroy($id);
header("location: {$_SERVER['PHP_SELF']}");
exit;
//列出所資料
case 'submission_index':
$where_arr = [];
Submission::index($where_arr, [], [], [], 20);
break;
//顯示某筆資料
case 'submission_show':
$where_arr['id'] = $id;
Submission::show($where_arr);
break;
//預設動作
default:
if (empty($id)) {
$where_arr = [];
Submission::index($where_arr, [], [], 20);
$op = 'submission_index';
} else {
$where_arr['id'] = $id;
Submission::show($where_arr);
$op = 'submission_show';
}
break;
}
index
(列出)、show
(顯示)、create
(表單)、store
(寫入)、update
(修改)、destroy
(刪除)index
(列出)、show
(顯示)、create
(表單) 需要有界面,因此,會有對應子樣板(名稱為 op_資料表_動作.tpl
依此類推,這不是官方規定,而是個人開發習慣)。store
(寫入)、update
(修改)、destroy
(刪除) 沒有界面,但執行完需要轉向,所以,可以用沒訊息的方式轉向:
header("location: {$_SERVER['PHP_SELF']}");
exit;
或者有訊息的方式轉向
redirect_header($_SERVER['PHP_SELF'], 3, "已成功執行!");
/*-----------秀出結果區--------------*/
$xoopsTpl->assign('toolbar', Utility::toolbar_bootstrap($interface_menu, false, $interface_icon));
$xoopsTpl->assign('now_op', $op);
$xoTheme->addStylesheet('modules/submission/css/module.css');
$xoTheme->addStylesheet('modules/tadtools/css/vtb.css');
require_once XOOPS_ROOT_PATH . '/footer.php';
$xoopsTpl->assign()
的方式,送一些必要的樣板標籤到樣板檔$xoTheme->addStylesheet()
來載入各式 css檔,可避免重複載入footer.php
檔,用來真正產生網站畫面。
/*-----------引入檔案區--------------*/
/*...略...*/
if (!$_SESSION['submission_adm']) {
redirect_header(XOOPS_URL . '/modules/submission/index.php', 3, _TAD_PERMISSION_DENIED);
}
更好的寫法:
if (! Tools::chk_is_adm('', '', __FILE__, __LINE__, 'return')) {
redirect_header("index.php", 3, "無操作權限");
}