bitrix-modules
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBitrix Modules
Bitrix模块
Identifier and Namespace
标识符与命名空间
- Identifier: (lowercase, no
<vendor>.<module>, no digit at start)._ - Installer class: (dot →
<vendor>_<module>)._ - Namespace: (dot →
\<Vendor>\<Module>\..., CamelCase) for partner modules with a dot in the id.\ - One-word module id (no partner prefix), e.g. : installer class
mymodule, PSR-4 namespacemymodule(Loader uses\Bitrix\Mymodule+Bitrix\), notucfirst($moduleName).\Mymodule
- 标识符:(小写,不含
<vendor>.<module>,首字符不为数字)。_ - 安装器类:(点替换为
<vendor>_<module>)。_ - 命名空间:对于ID包含点的合作伙伴模块,命名空间为(点替换为
\<Vendor>\<Module>\...,采用驼峰命名法)。\ - 单字模块ID(无合作伙伴前缀),例如:安装器类为
mymodule,PSR-4命名空间为**mymodule**(Loader使用\Bitrix\Mymodule+Bitrix\),而非ucfirst($moduleName)。\Mymodule
Quick Creation
快速创建
bash
php bitrix/bitrix.php make:module vendor.moduleSince main 25.900. On older versions, scaffold files manually.
make:module- ,
install/index.phpinstall/version.php - ,
install/mysql/install.sql(empty stubs)install/mysql/uninstall.sql default_option.phplang/ru/install/index.php
It does not create , , routes, or controllers. Add those yourself or via further / .
.settings.php/lib/make:*dev:module-skeletonbash
php bitrix/bitrix.php make:module vendor.module从主版本25.900开始支持。在旧版本中,需手动搭建文件结构。
make:module- ,
install/index.phpinstall/version.php - ,
install/mysql/install.sql(空占位文件)install/mysql/uninstall.sql default_option.phplang/ru/install/index.php
它不会创建, , 路由或控制器。这些内容需自行添加,或通过后续的 / 命令生成。
.settings.php/lib/make:*dev:module-skeletonMinimal Structure
最小化结构
/local/modules/vendor.module/
├── install/
│ ├── index.php
│ ├── version.php
│ └── mysql/ # optional SQL stubs from make:module
├── lang/ru/install/index.php
├── default_option.php
├── lib/ # PSR-4, Vendor\Module\... (add manually)
├── views/ # PHP views for renderView() in controllers
├── routes/ # Module route files — require from /local/routes/web.php
├── .settings.php # controllers, services, console (add manually)
└── include.php # optional, for registerNamespace/registerAutoLoadClasses/local/modules/vendor.module/
├── install/
│ ├── index.php
│ ├── version.php
│ └── mysql/ # make:module生成的可选SQL占位文件
├── lang/ru/install/index.php
├── default_option.php
├── lib/ # PSR-4规范,Vendor\Module\...(需手动添加)
├── views/ # 控制器中renderView()使用的PHP视图文件
├── routes/ # 模块路由文件 — 需要从/local/routes/web.php引入
├── .settings.php # 控制器、服务、控制台配置(需手动添加)
└── include.php # 可选,用于registerNamespace/registerAutoLoadClassesModule Routing
模块路由
Routing is global-only. The kernel loads route files listed in global from and only.
routing.config/local/routes//bitrix/routes/A section in the module's is not auto-loaded. Connect module routes by from :
routing.settings.phprequire/local/routes/web.phpphp
// /local/routes/web.php
return function (\Bitrix\Main\Routing\RoutingConfigurator $routes): void {
$moduleRoutes = $_SERVER['DOCUMENT_ROOT'] . '/local/modules/vendor.module/routes/web.php';
if (is_file($moduleRoutes))
{
(require $moduleRoutes)($routes);
}
};路由仅支持全局配置。内核仅加载全局中列出的、来自和的路由文件。
routing.config/local/routes//bitrix/routes/模块中的部分不会自动加载。需通过从引入模块路由:
.settings.phproutingrequire/local/routes/web.phpphp
// /local/routes/web.php
return function (\Bitrix\Main\Routing\RoutingConfigurator $routes): void {
$moduleRoutes = $_SERVER['DOCUMENT_ROOT'] . '/local/modules/vendor.module/routes/web.php';
if (is_file($moduleRoutes))
{
(require $moduleRoutes)($routes);
}
};install/version.php
install/version.phpinstall/version.php
install/version.phpphp
<?php
$arModuleVersion = [
'VERSION' => '1.0.0',
'VERSION_DATE' => '2026-04-16 12:00:00',
];php
<?php
$arModuleVersion = [
'VERSION' => '1.0.0',
'VERSION_DATE' => '2026-04-16 12:00:00',
];install/index.php
install/index.phpinstall/index.php
install/index.phpInherit from , implement /. Base template:
CModuleDoInstallDoUninstallphp
<?php
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\ModuleManager;
use Bitrix\Main\EventManager;
Loc::loadMessages(__FILE__);
final class vendor_module extends CModule
{
public $MODULE_ID = 'vendor.module';
public $MODULE_VERSION;
public $MODULE_VERSION_DATE;
public $MODULE_NAME;
public $MODULE_DESCRIPTION;
public $PARTNER_NAME = 'Vendor';
public $PARTNER_URI = 'https://vendor.example.com';
public function __construct()
{
$arModuleVersion = [];
include __DIR__ . '/version.php';
$this->MODULE_VERSION = $arModuleVersion['VERSION'] ?? '';
$this->MODULE_VERSION_DATE = $arModuleVersion['VERSION_DATE'] ?? '';
$this->MODULE_NAME = (string)Loc::getMessage('VENDOR_MODULE_NAME');
$this->MODULE_DESCRIPTION = (string)Loc::getMessage('VENDOR_MODULE_DESCRIPTION');
}
public function DoInstall(): void
{
global $USER, $APPLICATION;
if (!$USER->IsAdmin())
{
$APPLICATION->ThrowException('Access denied');
return;
}
ModuleManager::registerModule($this->MODULE_ID);
$this->installDb();
$this->installEvents();
$this->installAgents();
$this->installFiles();
}
public function DoUninstall(): void
{
global $USER;
if (!$USER->IsAdmin()) return;
$this->uninstallAgents();
$this->uninstallEvents();
$this->uninstallDb();
$this->uninstallFiles();
ModuleManager::unRegisterModule($this->MODULE_ID);
}
private function installDb(): void
{
// Table creation via ORM Entity:
// \Vendor\Module\Model\PostTable::getEntity()->createDbTable();
}
private function uninstallDb(): void
{
// Application::getConnection()->dropTable(PostTable::getTableName());
}
private function installEvents(): void
{
EventManager::getInstance()->registerEventHandler(
fromModule: 'main',
eventType: 'OnAfterUserAdd',
toModuleId: $this->MODULE_ID,
toClass: \Vendor\Module\Internals\Integration\Main\EventHandler\OnAfterUserAddHandler::class,
toMethod: 'handle',
);
}
private function uninstallEvents(): void
{
EventManager::getInstance()->unRegisterEventHandler(
fromModule: 'main',
eventType: 'OnAfterUserAdd',
toModuleId: $this->MODULE_ID,
toClass: \Vendor\Module\Internals\Integration\Main\EventHandler\OnAfterUserAddHandler::class,
toMethod: 'handle',
);
}
private function installAgents(): void
{
\CAgent::AddAgent(
\Vendor\Module\Cli\Agent\QueueAgent::class . '::run();',
$this->MODULE_ID,
'N',
300,
'',
'Y',
'',
100,
);
}
private function uninstallAgents(): void
{
\CAgent::RemoveModuleAgents($this->MODULE_ID);
}
private function installFiles(): void
{
CopyDirFiles(
__DIR__ . '/components',
$_SERVER['DOCUMENT_ROOT'] . '/local/components',
true,
true,
);
}
private function uninstallFiles(): void
{
DeleteDirFilesEx('/local/components/vendor');
}
}继承自,实现/方法。基础模板:
CModuleDoInstallDoUninstallphp
<?php
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\ModuleManager;
use Bitrix\Main\EventManager;
Loc::loadMessages(__FILE__);
final class vendor_module extends CModule
{
public $MODULE_ID = 'vendor.module';
public $MODULE_VERSION;
public $MODULE_VERSION_DATE;
public $MODULE_NAME;
public $MODULE_DESCRIPTION;
public $PARTNER_NAME = 'Vendor';
public $PARTNER_URI = 'https://vendor.example.com';
public function __construct()
{
$arModuleVersion = [];
include __DIR__ . '/version.php';
$this->MODULE_VERSION = $arModuleVersion['VERSION'] ?? '';
$this->MODULE_VERSION_DATE = $arModuleVersion['VERSION_DATE'] ?? '';
$this->MODULE_NAME = (string)Loc::getMessage('VENDOR_MODULE_NAME');
$this->MODULE_DESCRIPTION = (string)Loc::getMessage('VENDOR_MODULE_DESCRIPTION');
}
public function DoInstall(): void
{
global $USER, $APPLICATION;
if (!$USER->IsAdmin())
{
$APPLICATION->ThrowException('Access denied');
return;
}
ModuleManager::registerModule($this->MODULE_ID);
$this->installDb();
$this->installEvents();
$this->installAgents();
$this->installFiles();
}
public function DoUninstall(): void
{
global $USER;
if (!$USER->IsAdmin()) return;
$this->uninstallAgents();
$this->uninstallEvents();
$this->uninstallDb();
$this->uninstallFiles();
ModuleManager::unRegisterModule($this->MODULE_ID);
}
private function installDb(): void
{
// 通过ORM实体创建表:
// \Vendor\Module\Model\PostTable::getEntity()->createDbTable();
}
private function uninstallDb(): void
{
// Application::getConnection()->dropTable(PostTable::getTableName());
}
private function installEvents(): void
{
EventManager::getInstance()->registerEventHandler(
fromModule: 'main',
eventType: 'OnAfterUserAdd',
toModuleId: $this->MODULE_ID,
toClass: \Vendor\Module\Internals\Integration\Main\EventHandler\OnAfterUserAddHandler::class,
toMethod: 'handle',
);
}
private function uninstallEvents(): void
{
EventManager::getInstance()->unRegisterEventHandler(
fromModule: 'main',
eventType: 'OnAfterUserAdd',
toModuleId: $this->MODULE_ID,
toClass: \Vendor\Module\Internals\Integration\Main\EventHandler\OnAfterUserAddHandler::class,
toMethod: 'handle',
);
}
private function installAgents(): void
{
\CAgent::AddAgent(
\Vendor\Module\Cli\Agent\QueueAgent::class . '::run();',
$this->MODULE_ID,
'N',
300,
'',
'Y',
'',
100,
);
}
private function uninstallAgents(): void
{
\CAgent::RemoveModuleAgents($this->MODULE_ID);
}
private function installFiles(): void
{
CopyDirFiles(
__DIR__ . '/components',
$_SERVER['DOCUMENT_ROOT'] . '/local/components',
true,
true,
);
}
private function uninstallFiles(): void
{
DeleteDirFilesEx('/local/components/vendor');
}
}Language Files
语言文件
/local/modules/vendor.module/lang/ru/install/index.phpmake:modulephp
<?php
$MESS['VENDOR_MODULE_NAME'] = 'Vendor Module';
$MESS['VENDOR_MODULE_DESCRIPTION'] = 'Module description';Add (and other locales) as needed for multi-language admin UI.
lang/en/Lang file paths must mirror the source file path relative to the module root: → , → . / are read from these phrases in the installer constructor and shown in Admin → Settings → Product settings → Modules; if the lang file path or phrase codes don't match, the module appears there with an empty name/description.
/install/index.php/lang/<code>/install/index.php/admin/my_page.php/lang/<code>/admin/my_page.phpMODULE_NAMEMODULE_DESCRIPTION/local/modules/vendor.module/lang/ru/install/index.phpmake:modulephp
<?php
$MESS['VENDOR_MODULE_NAME'] = 'Vendor Module';
$MESS['VENDOR_MODULE_DESCRIPTION'] = 'Module description';如需多语言管理界面,可按需添加(及其他语言区域)。
lang/en/语言文件路径必须与源文件相对于模块根目录的路径保持一致: → , → 。 / 会在安装器构造函数中从这些短语中读取,并显示在管理面板的「设置 → 产品设置 → 模块」中;如果语言文件路径或短语代码不匹配,模块将在此处显示为空名称/描述。
/install/index.php/lang/<code>/install/index.php/admin/my_page.php/lang/<code>/admin/my_page.phpMODULE_NAMEMODULE_DESCRIPTIONDB Tables
数据库表
Do not use raw SQL for table creation. Describe the entity in and create the table via ORM:
/lib/Model/PostTable.phpphp
\Bitrix\Main\Loader::includeModule('vendor.module');
\Vendor\Module\Model\PostTable::getEntity()->createDbTable();For deletion:
php
\Bitrix\Main\Application::getConnection()->dropTable(PostTable::getTableName());不要使用原生SQL创建表。在中描述实体,然后通过ORM创建表:
/lib/Model/PostTable.phpphp
\Bitrix\Main\Loader::includeModule('vendor.module');
\Vendor\Module\Model\PostTable::getEntity()->createDbTable();删除表的代码:
php
\Bitrix\Main\Application::getConnection()->dropTable(PostTable::getTableName());Module Options (options.php
)
options.php模块选项(options.php
)
options.phpModule options ( + ) are for permanent settings. For TTL runtime state vs cache vs Option, see skill .
Optiondefault_option.phpbitrix-storageIf you need a settings page in Admin Panel (Settings → Module Settings → Vendor Module):
php
<?php
/** @var CMain $APPLICATION */
/** @var string $mid */ // module id
use Bitrix\Main\Config\Option;
use Bitrix\Main\Localization\Loc;
$options = [
['api_key', Loc::getMessage('VENDOR_API_KEY'), '', ['text', 40]],
['debug_mode', Loc::getMessage('VENDOR_DEBUG'), 'N', ['checkbox', 'Y']],
];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && check_bitrix_sessid())
{
foreach ($options as $opt)
{
$val = $_POST[$opt[0]] ?? $opt[2];
Option::set($mid, $opt[0], $val);
}
}
// ... display via CAdminTabControl模块选项( + )用于永久设置。关于TTL运行时状态、缓存与Option的对比,请参考技能。
Optiondefault_option.phpbitrix-storage如需在管理面板中添加设置页面(「设置 → 模块设置 → Vendor Module」):
php
<?php
/** @var CMain $APPLICATION */
/** @var string $mid */ // 模块ID
use Bitrix\Main\Config\Option;
use Bitrix\Main\Localization\Loc;
$options = [
['api_key', Loc::getMessage('VENDOR_API_KEY'), '', ['text', 40]],
['debug_mode', Loc::getMessage('VENDOR_DEBUG'), 'N', ['checkbox', 'Y']],
];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && check_bitrix_sessid())
{
foreach ($options as $opt)
{
$val = $_POST[$opt[0]] ?? $opt[2];
Option::set($mid, $opt[0], $val);
}
}
// ... 通过CAdminTabControl显示PSR-4 Autoloading
PSR-4自动加载
Nothing needs to be registered manually in if:
include.php- Module is in .
/local/modules/vendor.module/ - Classes are in .
/lib/ - Namespace follows (or
\Vendor\Module\...for a one-word id).\Bitrix\Mymodule\...
Bitrix handles this automatically when is called.
LoaderincludeModule满足以下条件时,无需在中手动注册:
include.php- 模块位于路径下。
/local/modules/vendor.module/ - 类文件位于目录中。
/lib/ - 命名空间遵循(对于单字ID,为
\Vendor\Module\...)。\Bitrix\Mymodule\...
当调用时,Bitrix的会自动处理自动加载。
includeModuleLoaderChecklist
检查清单
- Module identifier follows format (or one-word →
vendor.modulenamespace).\Bitrix\... - After ,
make:module/.settings.phpadded if needed (generator is minimal).lib/ - Module routes are d from
require— not expected from module/local/routes/web.php.settings.php.routing - /
DoInstallare implemented and idempotent.DoUninstall - Event handlers and agents are registered upon installation and removed upon uninstallation.
- DB tables are managed via ORM or (DDL).
SqlHelper - Language files exist where needed (from generator; add
lang/ru/etc.).lang/en/ - Services and controllers are registered in .
.settings.php - No hardcoded strings in (use
index.php).Loc - Module is compatible with PSR-4.
- Files are copied to , not
/local/./bitrix/
- 模块标识符遵循格式(单字ID对应
vendor.module命名空间)。\Bitrix\... - 使用后,按需添加
make:module/.settings.php(生成器仅创建最小结构)。lib/ - 模块路由通过中的
/local/routes/web.php引入 — 不会自动加载模块require中的.settings.php配置。routing - 实现了/
DoInstall方法,且具备幂等性。DoUninstall - 事件处理器与Agent在安装时注册,卸载时移除。
- 通过ORM或(DDL)管理数据库表。
SqlHelper - 按需存在语言文件(生成器创建了;可添加
lang/ru/等)。lang/en/ - 服务与控制器在中注册。
.settings.php - 中无硬编码字符串(使用
index.php)。Loc - 模块兼容PSR-4规范。
- 文件复制到目录,而非
/local/。/bitrix/