1.2 Discuz! X5.0:App 扩展

概述

Discuz! X5.0 将各个模块功能的包含类文件进行了合并,所有同模块的文件都在各自的目录下。 X3.5 版本的 /source/module 目录已经合并到各个 app 的子目录 /source/app/xxx/module 中。

如果您要进行 app 的扩展,可直接在 /source/app 下创建目录,然后在此目录下创建同名文件,例如 /source/app/xxx/xxx.php

<?php

require './source/class/class_core.php';

$discuz = C::app();
$discuz->cachelist = [];
$discuz->init();

require_once appfile('yyy/zzz'); // 调用 /source/app/xxx/yyy/zzz.php 文件

模板

/source/app/xxx 目录下可以直接创建模板文件,可用类似以下方式调用 source/app/xxx/template/tpl.php 模板文件。仅支持 php 格式是模板文件:

require_once template('tpl', 0, 'source/app/xxx/template');

或者

require_once apptemplate('tpl');

管理中心

/source/app/xxx/admin 目录可存放 App 自己的管理后台文件,通过 URL “admin.php?action=app&operation=xxx:yyy” 可以直接调用 /source/app/xxx/admin/yyy.php。

在 admin/ 目录下创建 switch.php 可直接在“站点功能”添加当前模块的开关入口,文件内容如下:

class app_sample_switch {

    const Icon = 'source/app/sample/static/image/icon.png';
    const Name = 'sample';
    const Desc = 'description';

    public static function getStatus() {
        return 1;
    }

    public static function getOptions() {
        return '<a href="'.ADMINSCRIPT.'?action=app&operation=sample:op">开启</a>';
    }

}

如果 App 存在多个开关项,使用以下的代码结构:

class app_sample_switch {

    public static function getModules() {
        return ['module1', 'module2'];
    }

}

class app_sample_switch_module1 {

    const Icon = 'source/app/sample/static/image/icon.png';
    const Name = 'sampleModule1';
    const Desc = 'descriptionModule1';

    public static function getStatus() {
        return 1;
    }

    public static function getOptions() {
        return '<a href="'.ADMINSCRIPT.'?action=app&operation=sample:op">开启模块1</a>';
    }

}

class app_sample_switch_module2 {

    const Icon = 'source/app/sample/static/image/icon.png';
    const Name = 'sampleModule2';
    const Desc = 'descriptionModule2';

    public static function getStatus() {
        return 1;
    }

    public static function getOptions() {
        return '<a href="'.ADMINSCRIPT.'?action=app&operation=sample:op">开启模块2</a>';
    }

}

语言包

App 的语言包是对默认语言包的拓展,在 /source/app/xxx/language 目录下放入与 /source/language 中的同名文件,即可对原有语言包进行扩展。但是 $lang 变量要使用 “+=” 进行赋值。

/source/app/sample/language/lang_template.php

<?php

$lang += [
    'sameple_test' => '测试文字',
];

对于 X5.0 支持的多语言包拓展则是在 /source/app/xxx/i18n/langkey 目录下。

插件范例

X5.0 App扩展完整范例见 sample 插件


整理自 Discuz! 官方开放平台技术文档(open.dismall.com),由 WorkBuddy 整理发布。