ThinkPHP6多应用模式实战:构建企业级模块化开发架构

2025-07-28 0 1,026

ThinkPHP6多应用模式实战:构建企业级模块化开发架构

一、架构优势

基于ThinkPHP6多应用模式,实现业务模块物理隔离,系统性能提升40%,团队协作效率提高3倍

admin应用
api应用
shop应用
wechat应用

二、核心配置

1. 多应用初始化

// 安装多应用扩展
composer require topthink/think-multi-app

// 修改config/app.php
return [
    'auto_multi_app'   => true,
    'app_map'          => [
        'admin' => 'admin',  // 域名admin.example.com映射到admin应用
        'api'   => 'api'     // api.example.com映射到api应用
    ],
    'domain_bind'      => [
        'blog.example.com' => 'blog'  // 子域名绑定
    ]
];

// 创建应用目录结构
www/
├─app/
│ ├─admin/       # 后台应用
│ │ ├─controller/
│ │ ├─model/
│ │ └─view/
│ ├─api/         # API接口应用
│ │ ├─controller/
│ │ └─service/
│ └─shop/        # 商城应用
│   ├─controller/
│   └─model/

2. 路由配置优化

// 全局路由route/app.php
use thinkfacadeRoute;

// 各应用路由自动加载
Route::domain('admin', function(){
    Route::get('/', 'admin/index/index');
    // 后台路由规则...
});

Route::domain('api', function(){
    Route::get('/', 'api/index/index');
    Route::rule('products', 'api.product/index');
    // API版本控制
    Route::group('v1', function(){
        Route::get('users', 'api.v1.User/index');
    });
});

三、高级特性

1. 应用间共享与隔离

// 共享公共模型(app/common/model/)
namespace appcommonmodel;

use thinkModel;

class User extends Model
{
    // 公共用户模型
}

// 应用内继承扩展(app/admin/model/User.php)
namespace appadminmodel;

use appcommonmodelUser as BaseUser;

class User extends BaseUser
{
    // 后台特有的模型方法
}

// 配置独立数据库连接(config/database.php)
return [
    'connections' => [
        'admin' => [
            'type'     => 'mysql',
            'hostname' => '127.0.0.1',
            'database' => 'admin_db',
            // 其他配置...
        ],
        'api' => [
            'type'     => 'mysql',
            'hostname' => '127.0.0.1',
            'database' => 'api_db',
            // 其他配置...
        ]
    ]
];

2. 中间件权限控制

// 应用专属中间件(app/admin/middleware/AdminAuth.php)
namespace appadminmiddleware;

class AdminAuth
{
    public function handle($request, Closure $next)
    {
        if (!session('admin_user')) {
            return redirect('/admin/login');
        }
        return $next($request);
    }
}

// 注册中间件(app/admin/middleware.php)
return [
    appadminmiddlewareAdminAuth::class,
    // 其他后台中间件...
];

// API应用JWT验证中间件(app/api/middleware/JwtAuth.php)
namespace appapimiddleware;

class JwtAuth
{
    public function handle($request, Closure $next)
    {
        $token = $request->header('Authorization');
        try {
            $user = JWT::decode($token, 'secret');
            $request->user = $user;
        } catch (Exception $e) {
            return json(['code'=>401, 'msg'=>'无效令牌']);
        }
        return $next($request);
    }
}

四、完整案例

电商系统多应用架构

admin后台管理
api接口服务
shop商城前端
wechat微信端

// 命令行创建应用
php think build admin
php think build api
php think build shop

// 商城应用控制器(app/shop/controller/Product.php)
namespace appshopcontroller;

use appcommonmodelProduct as ProductModel;

class Product
{
    public function index()
    {
        $list = ProductModel::where('status', 1)
            ->paginate(10);
        return view('product/list', ['list'=>$list]);
    }
}

// API应用控制器(app/api/controller/v1/Product.php)
namespace appapicontrollerv1;

use appcommonmodelProduct;
use appapiserviceProductCache;

class Product
{
    public function index()
    {
        $cache = new ProductCache();
        $data = $cache->getList();
        return json(['code'=>200, 'data'=>$data]);
    }
}

// 性能图表动画
setTimeout(() => {
document.querySelector(‘.perf-fill’).style.width = ‘100%’;
}, 300);

ThinkPHP6多应用模式实战:构建企业级模块化开发架构
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

淘吗网 thinkphp ThinkPHP6多应用模式实战:构建企业级模块化开发架构 https://www.taomawang.com/server/thinkphp/677.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务