XenForo 2 provides the ability to create and integrate custom modules to extend the functionality of your forum. Creating a custom module allows you to add special features that fit the needs of your community. This guide will walk you through the process of creating a custom module in XenForo 2.
Preparing the Development Environment
Before you start creating modules, you need to prepare your development environment. Make sure you have XenForo 2 installed and have access to your hosting server. You will also need a basic understanding of PHP, MySQL, and XenForo’s system structure.
Additionally, you need a source code editor like Visual Studio Code to easily edit and develop modules.
Create Module Folder
In the XenForo 2 root directory, navigate to src/addons. This is where installed modules are stored and also where you will create new modules. Create a subfolder with the name of the module you want to develop, for example: MyCustomModule.
Inside this new folder, create subfolders following the standard XenForo structure, including _data, Admin, Entity, Service, Pub, Repository.
JSON File Definition For Module
Each module needs a file. addon.json to define basic information. In the module directory, create a file addon.json with the following content:
{
"legacy_addon_id": "",
"title": "Tên Module Tùy Chỉnh",
"description": "Mô tả về module tùy chỉnh.",
"version_id": 1000010,
"version_string": "1.0.0",
"dev": "Tên nhà phát triển",
"dev_url": "//example.com",
"require": {
"XF": (2020070, "XenForo 2.2.0+")
}
}
This file helps XenForo identify modules, manage versions, and ensure compatibility with the system.
Create Model (Entity) and Database Tables
If your module needs to store its own data, create a model (Entity) and a corresponding database table. In the folder Entitycreate a PHP file representing the model, for example: CustomEntity.php.
You also need to create a SQL file to create tables in the database. In the folder _datacreate file install.sql and add the SQL statement to create the table, for example:
CREATE TABLE xf_custom_entity (
entity_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Creating Controllers
Controller files are responsible for handling requests from users and displaying the corresponding content. In the directory Pub/Controllercreate file CustomController.php to handle requests from users to the module.
For example, the controller file could return a list of data from the model and display it in the forum:
public function actionIndex()
{
$customRepo = $this->repository('MyCustomModule:CustomEntity');
$data = $customRepo->findAllEntities();
$viewParams = ('data' => $data);
return $this->view('MyCustomModule:CustomList', 'custom_list_template', $viewParams);
}
Custom Modules
Creating custom modules for XenForo 2 is a process that requires knowledge of programming and XenForo’s structure. By following the steps above, you can easily develop new features and extend the functionality of your forum in a flexible and professional way.
Hopefully this guide will help you get started developing custom modules for XenForo 2. Good luck building new features for your forum!