XMF Pembantu Modul
Kelas XMF\Module\Helper menyediakan cara mudah untuk mengakses maklumat berkaitan modul, konfigurasi, pengendali dan banyak lagi. Menggunakan pembantu modul memudahkan kod anda dan mengurangkan boilerplate.
Gambaran Keseluruhan
Section titled “Gambaran Keseluruhan”Pembantu modul menyediakan:
- Akses konfigurasi dipermudahkan
- Pengambilan semula objek modul
- Instalasi pengendali
- Laluan dan URL resolusi
- Kebenaran dan pembantu sesi
- Pengurusan cache
Mendapatkan Pembantu Modul
Section titled “Mendapatkan Pembantu Modul”Penggunaan Asas
Section titled “Penggunaan Asas”use Xmf\Module\Helper;
// Get helper for a specific module$helper = Helper::getHelper('mymodule');
// The helper is automatically associated with the module directoryDaripada Modul Semasa
Section titled “Daripada Modul Semasa”Jika anda tidak menentukan nama modul, ia menggunakan modul aktif semasa:
$helper = Helper::getHelper('');// or$helper = Helper::getHelper(basename(__DIR__));Akses Konfigurasi
Section titled “Akses Konfigurasi”Cara XOOPS Tradisional
Section titled “Cara XOOPS Tradisional”Mendapatkan konfigurasi modul dengan cara lama adalah verbose:
$module_handler = xoops_gethandler('module');$module = $module_handler->getByDirname('mymodule');$config_handler = xoops_gethandler('config');$moduleConfig = $config_handler->getConfigsByCat(0, $module->getVar('mid'));$value = isset($moduleConfig['foo']) ? $moduleConfig['foo'] : 'default';echo "The value of 'foo' is: " . $value;XMF Cara
Section titled “XMF Cara”Dengan pembantu modul, tugas yang sama menjadi mudah:
$helper = \Xmf\Module\Helper::getHelper('mymodule');echo "The value of 'foo' is: " . $helper->getConfig('foo', 'default');Kaedah Pembantu
Section titled “Kaedah Pembantu”getModule()
Section titled “getModule()”Mengembalikan objek XoopsModule untuk modul pembantu.
$module = $helper->getModule();$version = $module->getVar('version');$name = $module->getVar('name');$mid = $module->getVar('mid');getConfig($name, $default)
Section titled “getConfig($name, $default)”Mengembalikan nilai konfigurasi modul atau semua konfigurasi.
// Get single config with default$itemsPerPage = $helper->getConfig('items_per_page', 10);$enableCache = $helper->getConfig('enable_cache', true);
// Get all configs as array$allConfigs = $helper->getConfig('');getHandler($name)
Section titled “getHandler($name)”Mengembalikan pengendali objek untuk modul.
$itemHandler = $helper->getHandler('items');$categoryHandler = $helper->getHandler('categories');
// Use the handler$item = $itemHandler->get($id);$items = $itemHandler->getObjects($criteria);loadBahasa($name)
Section titled “loadBahasa($name)”Memuatkan fail bahasa untuk modul.
$helper->loadLanguage('main');$helper->loadLanguage('admin');$helper->loadLanguage('modinfo');isCurrentModule()
Section titled “isCurrentModule()”Menyemak sama ada modul ini ialah modul yang sedang aktif.
if ($helper->isCurrentModule()) { // We're in the module's own pages} else { // Called from another module or location}isUserAdmin()
Section titled “isUserAdmin()”Menyemak sama ada pengguna semasa mempunyai hak pentadbir untuk modul ini.
if ($helper->isUserAdmin()) { // Show admin options echo '<a href="' . $helper->url('admin/index.php') . '">Admin</a>';}Laluan dan URL Kaedah
Section titled “Laluan dan URL Kaedah”url($url)
Section titled “url($url)”Mengembalikan URL mutlak untuk laluan relatif modul.
$logoUrl = $helper->url('images/logo.png');// Returns: https://example.com/modules/mymodule/images/logo.png
$adminUrl = $helper->url('admin/index.php');// Returns: https://example.com/modules/mymodule/admin/index.phplaluan($path)
Section titled “laluan($path)”Mengembalikan laluan sistem fail mutlak untuk laluan relatif modul.
$templatePath = $helper->path('templates/view.tpl');$includePath = $helper->path('include/functions.php');require_once $includePath;muat naikUrl($url)
Section titled “muat naikUrl($url)”Mengembalikan URL mutlak untuk fail muat naik modul.
$fileUrl = $helper->uploadUrl('documents/manual.pdf');uploadPath($path)
Section titled “uploadPath($path)”Mengembalikan laluan sistem fail mutlak untuk fail muat naik modul.
$uploadDir = $helper->uploadPath('');$filePath = $helper->uploadPath('images/photo.jpg');ubah hala($url, $time, $message)
Section titled “ubah hala($url, $time, $message)”Mengubah hala dalam modul kepada relatif modul URL.
$helper->redirect('index.php', 3, 'Item saved successfully');$helper->redirect('view.php?id=' . $newId, 2, 'Created!');Sokongan Penyahpepijatan
Section titled “Sokongan Penyahpepijatan”setDebug($bool)
Section titled “setDebug($bool)”Dayakan atau lumpuhkan mod nyahpepijat untuk pembantu.
$helper->setDebug(true); // Enable$helper->setDebug(false); // Disable$helper->setDebug(); // Enable (default is true)addLog($log)
Section titled “addLog($log)”Tambahkan mesej pada log modul.
$helper->addLog('Processing item ID: ' . $id);$helper->addLog('Cache miss, loading from database');Kelas Pembantu Berkaitan
Section titled “Kelas Pembantu Berkaitan”XMF menyediakan pembantu khusus yang memanjangkan XMF\Module\Helper\AbstractHelper:
Pembantu Kebenaran
Section titled “Pembantu Kebenaran”Lihat ../Recipes/Permission-Helper untuk dokumentasi terperinci.
$permHelper = new \Xmf\Module\Helper\Permission('mymodule');
// Check permissionif ($permHelper->checkPermission('view', $itemId)) { // User has permission}
// Check and redirect if no permission$permHelper->checkPermissionRedirect('edit', $itemId, 'index.php', 3, 'Access denied');Pembantu Sesi
Section titled “Pembantu Sesi”Storan sesi sedar modul dengan awalan kunci automatik.
$session = new \Xmf\Module\Helper\Session('mymodule');
// Store value$session->set('last_viewed', $itemId);
// Retrieve value$lastViewed = $session->get('last_viewed', 0);
// Delete value$session->del('last_viewed');
// Clear all module session data$session->destroy();Pembantu Cache
Section titled “Pembantu Cache”Caching sedar modul dengan awalan kunci automatik.
$cache = new \Xmf\Module\Helper\Cache('mymodule');
// Write to cache (TTL in seconds)$cache->write('item_' . $id, $itemData, 3600);
// Read from cache$data = $cache->read('item_' . $id, null);
// Delete from cache$cache->delete('item_' . $id);
// Read with automatic regeneration$data = $cache->cacheRead( 'expensive_data', function() { // This runs only if cache miss return computeExpensiveData(); }, 3600);Contoh Lengkap
Section titled “Contoh Lengkap”Berikut ialah contoh komprehensif menggunakan pembantu modul:
<?phpuse Xmf\Request;use Xmf\Module\Helper;use Xmf\Module\Helper\Permission;use Xmf\Module\Helper\Session;
require_once dirname(dirname(__DIR__)) . '/mainfile.php';
// Initialize helpers$helper = Helper::getHelper('mymodule');$permHelper = new Permission('mymodule');$session = new Session('mymodule');
// Load language$helper->loadLanguage('main');
// Get configuration$itemsPerPage = $helper->getConfig('items_per_page', 10);$enableComments = $helper->getConfig('enable_comments', true);
// Handle request$op = Request::getCmd('op', 'list');$id = Request::getInt('id', 0);
require_once XOOPS_ROOT_PATH . '/header.php';
switch ($op) { case 'view': // Check permission if (!$permHelper->checkPermission('view', $id)) { redirect_header($helper->url('index.php'), 3, _NOPERM); }
// Track in session $session->set('last_viewed', $id);
// Get handler and item $itemHandler = $helper->getHandler('items'); $item = $itemHandler->get($id);
if (!$item) { redirect_header($helper->url('index.php'), 3, 'Item not found'); }
// Display item $xoopsTpl->assign('item', $item->toArray()); break;
case 'list': default: $itemHandler = $helper->getHandler('items');
$criteria = new CriteriaCompo(); $criteria->setLimit($itemsPerPage); $criteria->setSort('created'); $criteria->setOrder('DESC');
$items = $itemHandler->getObjects($criteria); $xoopsTpl->assign('items', $items);
// Show last viewed if exists $lastViewed = $session->get('last_viewed', 0); if ($lastViewed > 0) { $xoopsTpl->assign('last_viewed', $lastViewed); } break;}
// Admin link if authorizedif ($helper->isUserAdmin()) { $xoopsTpl->assign('admin_url', $helper->url('admin/index.php'));}
require_once XOOPS_ROOT_PATH . '/footer.php';Kelas Asas AbstractHelper
Section titled “Kelas Asas AbstractHelper”Semua XMF kelas pembantu melanjutkan XMF\Module\Helper\AbstractHelper, yang menyediakan:
Pembina
Section titled “Pembina”public function __construct($dirname)Instantiate dengan nama direktori modul. Jika kosong, gunakan modul semasa.
dirname()
Section titled “dirname()”Mengembalikan nama direktori modul yang dikaitkan dengan pembantu.
$dirname = $helper->dirname();init()
Section titled “init()”Dipanggil oleh pembina selepas modul dimuatkan. Gantikan dalam pembantu tersuai untuk logik permulaan.
Mencipta Pembantu Tersuai
Section titled “Mencipta Pembantu Tersuai”Anda boleh melanjutkan pembantu untuk kefungsian khusus modul:
<?phpnamespace XoopsModules\Mymodule;
class Helper extends \Xmf\Module\Helper\GenericHelper{ public function init() { // Custom initialization }
public function getItemUrl($id) { return $this->url('item.php?id=' . $id); }
public function getUploadDirectory() { $path = $this->uploadPath(''); if (!is_dir($path)) { mkdir($path, 0755, true); } return $path; }}Lihat Juga
Section titled “Lihat Juga”- Bermula-dengan-XMF - Asas XMF penggunaan
- XMF-Permintaan - Permintaan pengendalian
- ../Recipes/Permission-Helper - Pengurusan kebenaran
- ../Recipes/Module-Admin-Pages - Penciptaan antara muka pentadbir
#XMF #module-helper #konfigurasi #pengendali #sesi #cache