Appearance
模塊核心控制器 ?
您可以為模塊建立一個核心控制器文件,在該文件內,您可以準備好模塊的安裝、卸載、更新、啟用、禁用事件方法,在用戶對本模塊執行對應操作時,系統將自動執行對應的方法。
核心控制器文件名 ?
- 核心控制器是以模塊標識名進行首字母大寫的方式來命名的
| 模塊唯一標識 | 核心控制器文件名 | 類名 |
|---|---|---|
| test | Test | Test |
| userlogin | Userlogin | Userlogin |
| login1 | Login1 | Login1 |
核心控制器方法 ?
php
<?php
namespace modules\test;
use think\facade\Db;
use app\common\library\Menu;
use app\admin\model\AdminRule;
class Test
{
/**
* 安裝模塊時執行的方法
*/
public function install()
{
// 往后臺常規管理內添加一個菜單
$pMenu = AdminRule::where('name', 'routine')->value('id');
$menu = [
[
'type' => 'menu',
'title' => '通知公告管理',
'name' => 'routine/notice',
'path' => 'routine/notice',
'icon' => 'el-icon-ChatLineRound',
'menu_type' => 'tab',
'component' => '/src/views/backend/routine/notice/index.vue',
'keepalive' => '1',
'pid' => $pMenu ? $pMenu : 0,
'children' => [
['type' => 'button', 'title' => '查看', 'name' => 'routine/notice/index'],
['type' => 'button', 'title' => '添加', 'name' => 'routine/notice/add'],
['type' => 'button', 'title' => '編輯', 'name' => 'routine/notice/edit'],
['type' => 'button', 'title' => '刪除', 'name' => 'routine/notice/del'],
['type' => 'button', 'title' => '快速排序', 'name' => 'routine/notice/sortable'],
],
]
];
Menu::create($menu);
}
/**
* 卸載模塊時執行的方法
*/
public function uninstall()
{
// 刪除添加的菜單
Menu::delete('routine/notice', true);
}
/**
* 啟用模塊時執行的方法
*/
public function enable()
{
// 修改系統配置
Db::name('config')
->where('name', 'record_number')
->update([
'value' => '備案號-測試1',
]);
// 啟用模塊添加的菜單
Menu::enable('routine/notice');
// 假設模塊添加了單獨的配置菜單,下面這行代碼可以添加一個:系統配置 -> 快速配置入口
Config::addQuickEntrance('測試模塊配置', '/admin/test/config');
// 如果模塊的配置信息沒有保存于數據庫,可以利用緩存備份配置,避免模塊更新時配置數據丟失
$config = Cache::pull('test-module-config');
if ($config) {
@file_put_contents(config_path() . 'test.php', $config);
}
}
/**
* 禁用模塊時執行的方法
*/
public function disable()
{
// 禁用模塊添加的菜單
Menu::disable('routine/notice');
// 刪除系統配置 -> 快速配置入口
Config::removeQuickEntrance('測試模塊配置');
// 如果模塊的配置信息沒有保存于數據庫,可以利用緩存備份配置,避免模塊更新時配置數據丟失
$config = @file_get_contents(config_path() . 'test.php');
if ($config) {
Cache::set('test-module-config', $config, 3600);
}
}
/**
* 升級模塊時執行的方法
*/
public function update()
{
}
}
