Getting Started with XMF
2.5.x ✅ 4.0.x ✅
This guide covers the fundamental concepts of the XOOPS Module Framework (XMF) and how to start using it in your modules.
Prerequisites
Section titled “Prerequisites”- XOOPS 2.5.8 or later installed
- PHP 7.2 or later
- Basic understanding of PHP object-oriented programming
Understanding Namespaces
Section titled “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
Section titled “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
Section titled “Namespaces Solution”Namespaces create isolated naming contexts:
<?phpnamespace MyNamespace;
class ArrayObject { public function doStuff() { // ... }}// No conflict - this is \MyNamespace\ArrayObjectUsing XMF Namespaces
Section titled “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
Section titled “Autoloading”One of XMF’s greatest conveniences is automatic class loading. You never need to manually include XMF class files.
Traditional XOOPS Loading
Section titled “Traditional XOOPS Loading”The old way required explicit loading:
XoopsLoad('xoopsrequest');$cleanInput = XoopsRequest::getString('input', '');XMF Autoloading
Section titled “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
Section titled “Basic Usage Examples”Reading Request Input
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “See Also”- ../XMF-Framework - Framework overview
- ../Reference/JWT - JSON Web Token support
- ../Reference/Database - Database utilities
#xmf #getting-started #namespaces #autoloading #basics