bitrix-modules

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Bitrix Modules

Bitrix模块

Identifier and Namespace

标识符与命名空间

  • Identifier:
    <vendor>.<module>
    (lowercase, no
    _
    , no digit at start).
  • Installer class:
    <vendor>_<module>
    (dot →
    _
    ).
  • Namespace:
    \<Vendor>\<Module>\...
    (dot →
    \
    , CamelCase) for partner modules with a dot in the id.
  • One-word module id (no partner prefix), e.g.
    mymodule
    : installer class
    mymodule
    , PSR-4 namespace
    \Bitrix\Mymodule
    (Loader uses
    Bitrix\
    +
    ucfirst($moduleName)
    ), not
    \Mymodule
    .
  • 标识符:
    <vendor>.<module>
    (小写,不含
    _
    ,首字符不为数字)。
  • 安装器类:
    <vendor>_<module>
    (点替换为
    _
    )。
  • 命名空间:对于ID包含点的合作伙伴模块,命名空间为
    \<Vendor>\<Module>\...
    (点替换为
    \
    ,采用驼峰命名法)。
  • 单字模块ID(无合作伙伴前缀),例如
    mymodule
    :安装器类为
    mymodule
    ,PSR-4命名空间为**
    \Bitrix\Mymodule
    **(Loader使用
    Bitrix\
    +
    ucfirst($moduleName)
    ),而非
    \Mymodule

Quick Creation

快速创建

bash
php bitrix/bitrix.php make:module vendor.module
Since main 25.900. On older versions, scaffold files manually.
make:module
creates a minimal skeleton only:
  • install/index.php
    ,
    install/version.php
  • install/mysql/install.sql
    ,
    install/mysql/uninstall.sql
    (empty stubs)
  • default_option.php
  • lang/ru/install/index.php
It does not create
.settings.php
,
/lib/
, routes, or controllers. Add those yourself or via further
make:*
/
dev:module-skeleton
.
bash
php bitrix/bitrix.php make:module vendor.module
从主版本25.900开始支持。在旧版本中,需手动搭建文件结构。
make:module
仅创建最小化骨架:
  • install/index.php
    ,
    install/version.php
  • install/mysql/install.sql
    ,
    install/mysql/uninstall.sql
    (空占位文件)
  • default_option.php
  • lang/ru/install/index.php
它不会创建
.settings.php
,
/lib/
, 路由或控制器。这些内容需自行添加,或通过后续的
make:*
/
dev:module-skeleton
命令生成。

Minimal 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/registerAutoLoadClasses

Module Routing

模块路由

Routing is global-only. The kernel loads route files listed in global
routing.config
from
/local/routes/
and
/bitrix/routes/
only.
A
routing
section in the module's
.settings.php
is not auto-loaded. Connect module routes by
require
from
/local/routes/web.php
:
php
// /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.php
中的
routing
部分不会自动加载。需通过
require
/local/routes/web.php
引入模块路由:
php
// /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.php

php
<?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.php

Inherit from
CModule
, implement
DoInstall
/
DoUninstall
. Base template:
php
<?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');
    }
}
继承自
CModule
,实现
DoInstall
/
DoUninstall
方法。基础模板:
php
<?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.php
(created by
make:module
):
php
<?php
$MESS['VENDOR_MODULE_NAME'] = 'Vendor Module';
$MESS['VENDOR_MODULE_DESCRIPTION'] = 'Module description';
Add
lang/en/
(and other locales) as needed for multi-language admin UI.
Lang file paths must mirror the source file path relative to the module root:
/install/index.php
/lang/<code>/install/index.php
,
/admin/my_page.php
/lang/<code>/admin/my_page.php
.
MODULE_NAME
/
MODULE_DESCRIPTION
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.
/local/modules/vendor.module/lang/ru/install/index.php
(由
make:module
生成):
php
<?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.php
MODULE_NAME
/
MODULE_DESCRIPTION
会在安装器构造函数中从这些短语中读取,并显示在管理面板的「设置 → 产品设置 → 模块」中;如果语言文件路径或短语代码不匹配,模块将在此处显示为空名称/描述。

DB Tables

数据库表

Do not use raw SQL for table creation. Describe the entity in
/lib/Model/PostTable.php
and create the table via ORM:
php
\Bitrix\Main\Loader::includeModule('vendor.module');
\Vendor\Module\Model\PostTable::getEntity()->createDbTable();
For deletion:
php
\Bitrix\Main\Application::getConnection()->dropTable(PostTable::getTableName());
不要使用原生SQL创建表。在
/lib/Model/PostTable.php
中描述实体,然后通过ORM创建表:
php
\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

Module options (
Option
+
default_option.php
) are for permanent settings. For TTL runtime state vs cache vs Option, see skill
bitrix-storage
.
If 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
模块选项(
Option
+
default_option.php
)用于永久设置。关于TTL运行时状态、缓存与Option的对比,请参考技能
bitrix-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
include.php
if:
  1. Module is in
    /local/modules/vendor.module/
    .
  2. Classes are in
    /lib/
    .
  3. Namespace follows
    \Vendor\Module\...
    (or
    \Bitrix\Mymodule\...
    for a one-word id).
Bitrix
Loader
handles this automatically when
includeModule
is called.
满足以下条件时,无需在
include.php
中手动注册:
  1. 模块位于
    /local/modules/vendor.module/
    路径下。
  2. 类文件位于
    /lib/
    目录中。
  3. 命名空间遵循
    \Vendor\Module\...
    (对于单字ID,为
    \Bitrix\Mymodule\...
    )。
当调用
includeModule
时,Bitrix的
Loader
会自动处理自动加载。

Checklist

检查清单

  • Module identifier follows
    vendor.module
    format (or one-word →
    \Bitrix\...
    namespace).
  • After
    make:module
    ,
    .settings.php
    /
    lib/
    added if needed (generator is minimal).
  • Module routes are
    require
    d from
    /local/routes/web.php
    — not expected from module
    .settings.php
    routing
    .
  • DoInstall
    /
    DoUninstall
    are implemented and idempotent.
  • Event handlers and agents are registered upon installation and removed upon uninstallation.
  • DB tables are managed via ORM or
    SqlHelper
    (DDL).
  • Language files exist where needed (
    lang/ru/
    from generator; add
    lang/en/
    etc.).
  • Services and controllers are registered in
    .settings.php
    .
  • No hardcoded strings in
    index.php
    (use
    Loc
    ).
  • Module is compatible with PSR-4.
  • Files are copied to
    /local/
    , not
    /bitrix/
    .
  • 模块标识符遵循
    vendor.module
    格式(单字ID对应
    \Bitrix\...
    命名空间)。
  • 使用
    make:module
    后,按需添加
    .settings.php
    /
    lib/
    (生成器仅创建最小结构)。
  • 模块路由通过
    /local/routes/web.php
    中的
    require
    引入 — 不会自动加载模块
    .settings.php
    中的
    routing
    配置。
  • 实现了
    DoInstall
    /
    DoUninstall
    方法,且具备幂等性。
  • 事件处理器与Agent在安装时注册,卸载时移除。
  • 通过ORM或
    SqlHelper
    (DDL)管理数据库表。
  • 按需存在语言文件(生成器创建了
    lang/ru/
    ;可添加
    lang/en/
    等)。
  • 服务与控制器在
    .settings.php
    中注册。
  • index.php
    中无硬编码字符串(使用
    Loc
    )。
  • 模块兼容PSR-4规范。
  • 文件复制到
    /local/
    目录,而非
    /bitrix/