Początek pracy z XMF
2.5.x ✅ 4.0.x ✅
Ten przewodnik obejmuje fundamentalne koncepcje XOOPS Module Framework (XMF) i sposób jego używania w swoich modułach.
Prerequisites
Dział zatytułowany „Prerequisites”- XOOPS 2.5.8 or later installed
- PHP 7.2 or later
- Basic understanding of PHP object-oriented programming
Understanding Namespaces
Dział zatytułowany „Understanding Namespaces”XMF uses PHP namespaces to organize its classes and avoid naming conflicts. All XMF classes are in the Xmf namespace.
Global Space Problem
Dział zatytułowany „Global Space Problem”Without namespaces, all PHP classes share a global space. This can cause conflicts:
<?php// This would conflict with PHP's built-in ArrayObjectclass ArrayObject { public function doStuff() { // ... }}// Fatal error: Cannot redeclare class ArrayObjectNamespaces Solution
Dział zatytułowany „Namespaces Solution”Namespaces create isolated naming contexts:
<?phpnamespace MyNamespace;
class ArrayObject { public function doStuff() { // ... }}// No conflict - this is \MyNamespace\ArrayObjectUsing XMF Namespaces
Dział zatytułowany „Using XMF Namespaces”You can reference XMF classes in several ways:
Full namespace path:
$helper = \Xmf\Module\Helper::getHelper('mymodule');With use statement:
use Xmf\Module\Helper;
$helper = Helper::getHelper('mymodule');Multiple imports:
use Xmf\Request;use Xmf\Module\Helper;use Xmf\Module\Helper\Permission;
$input = Request::getString('input', '');$helper = Helper::getHelper('mymodule');$perm = new Permission();Autoloading
Dział zatytułowany „Autoloading”One of XMF’s greatest conveniences is automatic class loading. You never need to manually include XMF class files.
Traditional XOOPS Loading
Dział zatytułowany „Traditional XOOPS Loading”The old way required explicit loading:
XoopsLoad('xoopsrequest');$cleanInput = XoopsRequest::getString('input', '');XMF Autoloading
Dział zatytułowany „XMF Autoloading”With XMF, classes load automatically when referenced:
$input = Xmf\Request::getString('input', '');Or with a use statement:
use Xmf\Request;
$input = Request::getString('input', '');$id = Request::getInt('id', 0);$op = Request::getCmd('op', 'display');The autoloader follows the PSR-4 standard and also manages dependencies that XMF relies on.
Basic Usage Examples
Dział zatytułowany „Basic Usage Examples”Reading Request Input
Dział zatytułowany „Reading Request Input”use Xmf\Request;
// Get integer value with default of 0$id = Request::getInt('id', 0);
// Get string value with default empty string$title = Request::getString('title', '');
// Get command (alphanumeric, lowercase)$op = Request::getCmd('op', 'list');
// Get email with validation$email = Request::getEmail('email', '');
// Get from specific hash (POST, GET, etc.)$formData = Request::getString('data', '', 'POST');Using the Module Helper
Dział zatytułowany „Using the Module Helper”use Xmf\Module\Helper;
// Get helper for your module$helper = Helper::getHelper('mymodule');
// Read module configuration$itemsPerPage = $helper->getConfig('items_per_page', 10);$enableFeature = $helper->getConfig('enable_feature', false);
// Access the module object$module = $helper->getModule();$version = $module->getVar('version');
// Get a handler$itemHandler = $helper->getHandler('items');
// Load language file$helper->loadLanguage('admin');
// Check if current moduleif ($helper->isCurrentModule()) { // We are in this module}
// Check admin rightsif ($helper->isUserAdmin()) { // User has admin access}Path and URL Helpers
Dział zatytułowany „Path and URL Helpers”use Xmf\Module\Helper;
$helper = Helper::getHelper('mymodule');
// Get module URL$moduleUrl = $helper->url('images/logo.png');// Returns: https://example.com/modules/mymodule/images/logo.png
// Get module path$modulePath = $helper->path('templates/view.tpl');// Returns: /var/www/html/modules/mymodule/templates/view.tpl
// Upload paths$uploadUrl = $helper->uploadUrl('files/document.pdf');$uploadPath = $helper->uploadPath('files/document.pdf');Debugging with XMF
Dział zatytułowany „Debugging with XMF”XMF provides helpful debugging tools:
// Dump a variable with nice formatting\Xmf\Debug::dump($myVariable);
// Dump multiple variables\Xmf\Debug::dump($var1, $var2, $var3);
// Dump POST data\Xmf\Debug::dump($_POST);
// Show a backtrace\Xmf\Debug::backtrace();The debug output is collapsible and displays objects and arrays in an easy-to-read format.
Project Structure Recommendation
Dział zatytułowany „Project Structure Recommendation”When building XMF-based modules, organize your code:
mymodule/ admin/ index.php menu.php class/ Helper.php # Optional custom helper ItemHandler.php # Your handlers include/ common.php language/ english/ main.php admin.php modinfo.php templates/ mymodule_index.tpl index.php xoops_version.phpCommon Include Pattern
Dział zatytułowany „Common Include Pattern”A typical module entry point:
<?phpuse Xmf\Request;use Xmf\Module\Helper;
require_once dirname(dirname(__DIR__)) . '/mainfile.php';
$helper = Helper::getHelper(basename(__DIR__));
// Get operation from request$op = Request::getCmd('op', 'list');$id = Request::getInt('id', 0);
// Include XOOPS headerrequire_once XOOPS_ROOT_PATH . '/header.php';
// Your module logic hereswitch ($op) { case 'view': // Handle view break; case 'list': default: // Handle list break;}
// Include XOOPS footerrequire_once XOOPS_ROOT_PATH . '/footer.php';Next Steps
Dział zatytułowany „Next Steps”Now that you understand the basics, explore:
- XMF-Request - Detailed request handling documentation
- XMF-Module-Helper - Complete module helper reference
- ../Recipes/Permission-Helper - Managing user permissions
- ../Recipes/Module-Admin-Pages - Building admin interfaces
See Also
Dział zatytułowany „See Also”- ../XMF-Framework - Framework overview
- ../Reference/JWT - JSON Web Token support
- ../Reference/Database - Database utilities
#xmf #getting-started #namespaces #autoloading #basics