“XMF模区块助手”
XMF\Module\Helper类提供了一种访问模区块-related信息、配置、处理程序等的简单方法。使用模区块助手可以简化代码并减少样板文件。
模区块助手提供:
- 简化的配置访问
- 模区块对象检索
- 处理程序实例化
- 路径和URL分辨率
- 权限和会话助手
- 缓存管理
获取模区块助手
Section titled “获取模区块助手”use Xmf\Module\Helper;
// Get helper for a specific module$helper = Helper::getHelper('mymodule');
// The helper is automatically associated with the module directory来自当前模区块
Section titled “来自当前模区块”如果不指定模区块名称,它将使用当前活动模区块:
$helper = Helper::getHelper('');// or$helper = Helper::getHelper(basename(__DIR__));传统XOOPS方式
Section titled “传统XOOPS方式”以旧方式获取模区块配置很冗长:
$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;使用模区块助手,相同的任务变得简单:
$helper = \Xmf\Module\Helper::getHelper('mymodule');echo "The value of 'foo' is: " . $helper->getConfig('foo', 'default');getModule()
Section titled “getModule()”返回助手模区块的 XOOPSModule 对象。
$module = $helper->getModule();$version = $module->getVar('version');$name = $module->getVar('name');$mid = $module->getVar('mid');getConfig($name, $default)
Section titled “getConfig($name, $default)”返回模区块配置值或所有配置。
// 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)”返回模区块的对象处理程序。
$itemHandler = $helper->getHandler('items');$categoryHandler = $helper->getHandler('categories');
// Use the handler$item = $itemHandler->get($id);$items = $itemHandler->getObjects($criteria);loadLanguage($name)
Section titled “loadLanguage($name)”加载模区块的语言文件。
$helper->loadLanguage('main');$helper->loadLanguage('admin');$helper->loadLanguage('modinfo');isCurrentModule()
Section titled “isCurrentModule()”检查该模区块是否是当前活动的模区块。
if ($helper->isCurrentModule()) { // We're in the module's own pages} else { // Called from another module or location}isUserAdmin()
Section titled “isUserAdmin()”检查当前用户是否具有该模区块的管理员权限。
if ($helper->isUserAdmin()) { // Show admin options echo '<a href="' . $helper->url('admin/index.php') . '">Admin</a>';}路径和 URL 方法
Section titled “路径和 URL 方法”网址($url)
Section titled “网址($url)”返回模区块-relative路径的绝对URL。
$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路径($path)
Section titled “路径($path)”返回模区块-relative路径的绝对文件系统路径。
$templatePath = $helper->path('templates/view.tpl');$includePath = $helper->path('include/functions.php');require_once $includePath;uploadUrl($url)
Section titled “uploadUrl($url)”返回模区块上传文件的绝对URL。
$fileUrl = $helper->uploadUrl('documents/manual.pdf');上传路径($path)
Section titled “上传路径($path)”返回模区块上传文件的绝对文件系统路径。
$uploadDir = $helper->uploadPath('');$filePath = $helper->uploadPath('images/photo.jpg');重定向($url、$time、$message)
Section titled “重定向($url、$time、$message)”在模区块内重定向到模区块-relativeURL。
$helper->redirect('index.php', 3, 'Item saved successfully');$helper->redirect('view.php?id=' . $newId, 2, 'Created!');setDebug($bool)
Section titled “setDebug($bool)”启用或禁用帮助程序的调试模式。
$helper->setDebug(true); // Enable$helper->setDebug(false); // Disable$helper->setDebug(); // Enable (default is true)addLog($log)
Section titled “addLog($log)”将消息添加到模区块日志。
$helper->addLog('Processing item ID: ' . $id);$helper->addLog('Cache miss, loading from database');XMF 提供扩展 XMF\Module\Helper\AbstractHelper 的专业帮助程序:
有关详细文档,请参阅../Recipes/Permission-Helper。
$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');具有自动密钥前缀的模区块-aware会话存储。
$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();模区块-aware具有自动键前缀的缓存。
$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);这是使用模区块助手的综合示例:
<?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';AbstractHelper 基类
Section titled “AbstractHelper 基类”所有 XMF 帮助程序类都扩展了 XMF\Module\Helper\AbstractHelper,它提供:
public function __construct($dirname)使用模区块目录名称实例化。如果为空,则使用当前模区块。
dirname()
Section titled “dirname()”返回与助手关联的模区块目录名称。
$dirname = $helper->dirname();init()
Section titled “init()”模区块加载后由构造函数调用。重写自定义帮助程序的初始化逻辑。
创建自定义助手
Section titled “创建自定义助手”您可以扩展模区块-specific功能的帮助程序:
<?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; }}- 获取-Started-with-XMF - 基本XMF用法
- XMF-Request - 请求处理
- ../Recipes/Permission-Helper - 权限管理
- ../Recipes/Module-Admin-Pages - 管理界面创建
#xmf#模区块-helper#configuration #handlers #session #cache