Bỏ qua để đến nội dung

Trình trợ giúp mô-đun XMF

Xmf\Module\Helper class cung cấp một cách dễ dàng để truy cập thông tin, cấu hình, trình xử lý, v.v. liên quan đến mô-đun. Việc sử dụng trình trợ giúp mô-đun sẽ đơn giản hóa mã của bạn và giảm bớt bản soạn sẵn.

Trình trợ giúp mô-đun cung cấp:

  • Truy cập cấu hình đơn giản hóa
  • Truy xuất đối tượng mô-đun
  • Khởi tạo trình xử lý
  • Đường dẫn và độ phân giải URL
  • Người trợ giúp quyền và phiên
  • Quản lý bộ đệm
use Xmf\Module\Helper;
// Get helper for a specific module
$helper = Helper::getHelper('mymodule');
// The helper is automatically associated with the module directory

Nếu bạn không chỉ định tên mô-đun, nó sẽ sử dụng mô-đun đang hoạt động hiện tại:

$helper = Helper::getHelper('');
// or
$helper = Helper::getHelper(basename(__DIR__));

Bắt cấu hình mô-đun theo cách cũ là dài dòng:

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

Với trình trợ giúp mô-đun, nhiệm vụ tương tự trở nên đơn giản:

$helper = \Xmf\Module\Helper::getHelper('mymodule');
echo "The value of 'foo' is: " . $helper->getConfig('foo', 'default');

Trả về đối tượng XoopsModule cho mô-đun của trình trợ giúp.

$module = $helper->getModule();
$version = $module->getVar('version');
$name = $module->getVar('name');
$mid = $module->getVar('mid');

Trả về giá trị cấu hình mô-đun hoặc tất cả các cấu hình.

// 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('');

Trả về một trình xử lý đối tượng cho mô-đun.

$itemHandler = $helper->getHandler('items');
$categoryHandler = $helper->getHandler('categories');
// Use the handler
$item = $itemHandler->get($id);
$items = $itemHandler->getObjects($criteria);

Tải tệp language cho mô-đun.

$helper->loadLanguage('main');
$helper->loadLanguage('admin');
$helper->loadLanguage('modinfo');

Kiểm tra xem mô-đun này có phải là mô-đun hiện đang hoạt động hay không.

if ($helper->isCurrentModule()) {
// We're in the module's own pages
} else {
// Called from another module or location
}

Kiểm tra xem người dùng hiện tại có quyền admin đối với mô-đun này hay không.

if ($helper->isUserAdmin()) {
// Show admin options
echo '<a href="' . $helper->url('admin/index.php') . '">Admin</a>';
}

Trả về URL tuyệt đối cho đường dẫn tương đối mô-đun.

$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.php

đường dẫn ###($path)

Trả về đường dẫn hệ thống tệp tuyệt đối cho đường dẫn tương đối của mô-đun.

/var/www/html/modules/mymodule/templates/view.tpl
$templatePath = $helper->path('templates/view.tpl');
$includePath = $helper->path('include/functions.php');
require_once $includePath;

Trả về URL tuyệt đối cho các tệp tải lên mô-đun.

$fileUrl = $helper->uploadUrl('documents/manual.pdf');

Trả về đường dẫn hệ thống tệp tuyệt đối cho các tệp tải lên mô-đun.

$uploadDir = $helper->uploadPath('');
$filePath = $helper->uploadPath('images/photo.jpg');

Chuyển hướng trong mô-đun tới URL tương đối với mô-đun.

$helper->redirect('index.php', 3, 'Item saved successfully');
$helper->redirect('view.php?id=' . $newId, 2, 'Created!');

Bật hoặc tắt chế độ gỡ lỗi cho người trợ giúp.

$helper->setDebug(true); // Enable
$helper->setDebug(false); // Disable
$helper->setDebug(); // Enable (default is true)

Thêm thông báo vào nhật ký mô-đun.

$helper->addLog('Processing item ID: ' . $id);
$helper->addLog('Cache miss, loading from database');

XMF cung cấp các công cụ trợ giúp chuyên dụng mở rộng Xmf\Module\Helper\AbstractHelper:

Xem ../Recipes/Permission-Helper để biết tài liệu chi tiết.

$permHelper = new \Xmf\Module\Helper\Permission('mymodule');
// Check permission
if ($permHelper->checkPermission('view', $itemId)) {
// User has permission
}
// Check and redirect if no permission
$permHelper->checkPermissionRedirect('edit', $itemId, 'index.php', 3, 'Access denied');

Lưu trữ phiên nhận biết mô-đun với tiền tố khóa tự động.

$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();

Bộ nhớ đệm nhận biết mô-đun với tiền tố khóa tự động.

$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
);

Đây là một ví dụ toàn diện về cách sử dụng trình trợ giúp mô-đun:

<?php
use 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 authorized
if ($helper->isUserAdmin()) {
$xoopsTpl->assign('admin_url', $helper->url('admin/index.php'));
}
require_once XOOPS_ROOT_PATH . '/footer.php';

Lớp cơ sở của trình trợ giúp trừu tượng

Phần tiêu đề “Lớp cơ sở của trình trợ giúp trừu tượng”

Tất cả trình trợ giúp XMF classes mở rộng Xmf\Module\Helper\AbstractHelper, cung cấp:

public function __construct($dirname)

Khởi tạo bằng tên thư mục mô-đun. Nếu trống, sử dụng mô-đun hiện tại.

Trả về tên thư mục mô-đun được liên kết với trình trợ giúp.

$dirname = $helper->dirname();

init()Được hàm tạo gọi sau khi mô-đun được tải. Ghi đè trong trình trợ giúp tùy chỉnh cho logic khởi tạo.

Phần tiêu đề “init()Được hàm tạo gọi sau khi mô-đun được tải. Ghi đè trong trình trợ giúp tùy chỉnh cho logic khởi tạo.”

Bạn có thể mở rộng trình trợ giúp cho chức năng dành riêng cho mô-đun:

mymodule/class/Helper.php
<?php
namespace 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;
}
}
  • Bắt đầu với XMF - Cách sử dụng XMF cơ bản
  • XMF-Request - Xử lý yêu cầu
  • ../Recipes/Permission-Helper - Quản lý quyền
  • ../Recipes/Module-Admin-Pages - Tạo giao diện quản trị

#xmf #module-helper #configuration #handlers #session #cache