当前位置:网站首页>phalapi框架改进方案,在一套phalapi系统上,管理多套api应用
phalapi框架改进方案,在一套phalapi系统上,管理多套api应用
2022-06-09 15:57:00 【木鱼大叔】
版本:PhalApi 2.2.3
请自行安装好PhalApi,然后按照步骤,作如下修改。
修改思路:在src/app/Api目录下,根据需要,创建若干个api应用,每个应用以文件夹的形式存在,然后根据每个api的应用名称,创建对应的数据库配置文件。
涉及到的文件如下:
修改1:config/di.php
原来的notorm实例化操作如下:
// 数据操作 - 基于NotORM
$di->notorm = new NotORMDatabase($di->config->get('dbs'), $di->debug);将上面的实例化操作,替换为如下代码:
// 获取当前api使用的数据库配置
$curDbConfig = 'dbs';
$appService = isset($_REQUEST['service']) ? $_REQUEST['service'] : $_REQUEST['s'];
// 有子命名空间时,使用该子命名空间对应的数据库,规则:config/db_子命名空间.php
if (strpos($appService,'_') !== false) {
$serviceTmp = explode('.',$appService);
$className = explode('_',($serviceTmp[0] == 'App' ? $serviceTmp[1] : $serviceTmp[0]));
$curDbConfig = sprintf('db_%s',strtolower($className[0]));
}
define('CURRENT_DB_CONFIG',$curDbConfig);
// 数据操作 - 基于NotORM
$di->notorm = new NotORMDatabase($di->config->get($curDbConfig), $di->debug);修改2:vendor/phalapi/kernal/src/Model/NotORMModel.php
修改其中的 loadTableKeys 方法,替换内容如下:
protected function loadTableKeys() {
$tables = \PhalApi\DI()->config->get(sprintf('%s.tables',CURRENT_DB_CONFIG));
if (empty($tables)) {
throw new InternalServerErrorException(\PhalApi\T(sprintf('%s.tables should not be empty',CURRENT_DB_CONFIG)));
}
foreach ($tables as $tableName => $tableConfig) {
static::$tableKeys[$tableName] = $tableConfig['key'];
// 分表的主键
foreach ($tableConfig['map'] as $mapItem) {
if (!isset($mapItem['start']) || !isset($mapItem['end'])) {
continue;
}
for ($i = $mapItem['start']; $i <= $mapItem['end']; $i ++) {
static::$tableKeys[$tableName . '_' . $i] = $tableConfig['key'];
}
}
}
}修改3:在config目录下,创建每个api应用对应的数据库配置文件,配置文件的命名规则为:db_应用名称.php,内容可以参考原来的dbs.php文件。

修改4:在src/app/Api目录下,根据需要,创建若干个api应用,每个应用,对应一个文件夹。目录结构如下:

至此,phalapi已可以支持多个数据库多套api应用的管理,最后看下文档列表界面:

边栏推荐
猜你喜欢
随机推荐
Wechat applet mind map
R language plot visualization: plot to visualize the two-dimensional histogram contour map, and add two variable edge histograms (2D histogram contour subplot) on the top and right of the two-dimensio
Ffmpeg mind map
QScrollArea使用教程之实现可上下滑动的设置界面
Read the middle office architecture and Implementation
容器和镜像的区别
ESP32-C3单火线智能开关,赋能传统开关智能化升级
Data security is urgent. What is the significance of the first SOC 2 authentication report for domestic intelligent manufacturers?
DAC8560的用法
Byte side: how to check if the website cannot be displayed?
June training (day 07) - hash table
Experience sharing of technical we media realization -- starting to try to make CSDN's reply one year later
Completion of xcm integration between Moonriver network and Calamari network
618. How to prepare for the great promotion
Ruoyi mind map
JPEX推出BAYC MAYC合约系列 欢迎体验
疫情之下,四点认知助你成长!
Dapr . Net core example
合约安全之-变量隐藏安全问题分析
LINQ之LEFT JOIN示例








