本文目录导读:

我来详细介绍 PHP Webman Admin 的使用方法。
什么是 Webman Admin
Webman Admin 是基于 Webman 框架开发的后台管理系统,它结合了:
- Webman:高性能 PHP 框架(基于 Workerman)
- Vue 3 + Element Plus:前端界面
- RBAC 权限管理
快速开始
安装环境要求
# 基础环境 PHP >= 8.0 Composer # 可选:Redis(用于缓存)
创建项目
# 创建 Webman 项目 composer create-project workerman/webman admin-demo cd admin-demo # 安装 Webman Admin composer require xiaodit/think-admin
数据库配置
// config/thinkorm.php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'type' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'webman_admin',
'username' => 'root',
'password' => 'your_password',
'charset' => 'utf8mb4',
'prefix' => '',
]
]
];
安装初始化
# 运行安装命令(Windows 用 start.bat 启动后访问) php think admin:install # 或直接访问初始化地址 # http://localhost:8787/admin/install
启动项目
# Linux/Mac php start.php start # Windows start.bat # 访问后台 # http://localhost:8787/admin
默认账号密码
安装完成后默认账号:
- 用户名:admin
- 密码:123456
核心功能模块
权限管理(RBAC)
// 控制器中配置权限
use support\Request;
use Webman\Admin\Middleware\AdminAuth;
class UserController
{
// 在控制器中设置权限
protected $auth = [
'index' => '用户列表',
'create' => '创建用户',
'update' => '更新用户',
'delete' => '删除用户'
];
// 使用中间件
public function middleware()
{
return [
AdminAuth::class
];
}
}
创建后台页面
// app/admin/controller/DemoController.php
namespace app\admin\controller;
use support\Request;
use Webman\Admin\Curd\BaseCurd;
class DemoController extends BaseCurd
{
// 模型
protected $model = \app\model\Demo::class;
// 列表字段配置
public function getListConfig()
{
return [
'title' => '示例管理',
'columns' => [
'id' => ['label' => 'ID'],
'title' => ['label' => '标题'],
'content' => ['label' => '内容'],
'status' => [
'label' => '状态',
'type' => 'switch',
'options' => [1 => '启用', 0 => '禁用']
],
'created_at' => ['label' => '创建时间']
],
'search' => [
'title' => ['type' => 'input', 'label' => '标题'],
'status' => ['type' => 'select', 'label' => '状态', 'options' => [1 => '启用', 0 => '禁用']]
]
];
}
// 表单配置
public function getFormConfig()
{
return [
'title' => '添加/编辑',
'fields' => [
'title' => [
'label' => '标题',
'type' => 'input',
'require' => true,
'placeholder' => '请输入标题'
],
'content' => [
'label' => '内容',
'type' => 'textarea',
'require' => true
],
'status' => [
'label' => '状态',
'type' => 'switch',
'default' => 1
]
]
];
}
}
路由配置
// config/route.php
use Webman\Route;
Route::group('/admin', function () {
// 后台路由
Route::any('demo/index', [app\admin\controller\DemoController::class, 'index']);
Route::any('demo/create', [app\admin\controller\DemoController::class, 'create']);
Route::any('demo/update', [app\admin\controller\DemoController::class, 'update']);
Route::post('demo/delete', [app\admin\controller\DemoController::class, 'delete']);
})->middleware([
Webman\Admin\Middleware\AdminAuth::class,
Webman\Admin\Middleware\Permission::class
]);
菜单配置
// config/admin/menu.php
return [
[
'title' => '系统管理',
'icon' => 'setting',
'children' => [
[
'title' => '用户管理',
'url' => '/admin/user/index',
'icon' => 'user'
],
[
'title' => '角色管理',
'url' => '/admin/role/index',
'icon' => 'lock'
]
]
],
[
'title' => '内容管理',
'icon' => 'document',
'children' => [
[
'title' => '文章管理',
'url' => '/admin/article/index',
'icon' => 'document'
]
]
]
];
自定义 API 接口
// app/controller/ApiController.php
namespace app\controller;
use support\Request;
class ApiController
{
// 获取列表数据
public function getList(Request $request)
{
$page = $request->get('page', 1);
$limit = $request->get('limit', 10);
$list = Db::table('demo')
->paginate($limit, [], ['page' => $page]);
return json([
'code' => 0,
'msg' => 'success',
'data' => $list
]);
}
// 添加数据
public function add(Request $request)
{
$data = $request->post();
$id = Db::table('demo')->insertGetId($data);
return json([
'code' => 0,
'msg' => '添加成功',
'data' => ['id' => $id]
]);
}
}
自定义前端页面
// public/admin/src/views/CustomView.vue
<template>
<div class="custom-container">
<el-card>
<el-button type="primary" @click="handleClick">点击我</el-button>
</el-card>
</div>
</template>
<script>
export default {
name: 'CustomView',
methods: {
handleClick() {
this.$message.success('自定义页面操作成功');
}
}
};
</script>
高级配置
多应用支持
# 创建多应用 php think app:create api # 创建 API 应用 php think app:create admin # 创建后台应用
缓存配置
// config/cache.php
return [
'default' => 'redis',
'stores' => [
'redis' => [
'driver' => 'redis',
'connection' => 'default'
],
'file' => [
'driver' => 'file',
'path' => runtime_path() . '/cache'
]
]
];
常见问题解决
登录超时
// config/admin/config.php
return [
'auth' => [
'token_expire' => 3600, // 设置 token 过期时间
'refresh_expire' => 7200, // 刷新 token 过期时间
]
];
上传配置
// config/admin/upload.php
return [
'image' => [
'max_size' => 2097152, // 2MB
'allow_types' => ['jpg', 'jpeg', 'png', 'gif'],
'path' => '/uploads'
],
'file' => [
'max_size' => 10485760, // 10MB
'allow_types' => ['pdf', 'doc', 'xls', 'zip'],
'path' => '/files'
]
];
邮件发送
// config/mail.php
return [
'host' => 'smtp.qq.com',
'port' => 465,
'username' => 'your-email@qq.com',
'password' => 'your-password',
'encryption' => 'ssl'
];
部署要点
生产环境部署
# 1. 优化配置 php think optimize:config php think optimize:route # 2. 启动生产模式(Linux) php start.php start -d # 后台守护进程 # 3. 配置 Nginx 反向代理
# nginx 配置示例
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:8787;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /ws {
proxy_pass http://127.0.0.1:8787;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
推荐使用场景
- 中小型后台管理系统:适合快速搭建企业后台
- API 接口服务:高效处理并发请求
- 实时应用:WebSocket 支持好
- 微服务架构:与其他服务集成
Webman Admin 结合了 Webman 的高性能和成熟的管理后台功能,适合快速开发和部署后台系统,如果遇到具体问题,可以查看官方文档或在社区寻求帮助。