🚀 基于 SlimPHP 的现代化 PHP Web 框架。一个轻量级、高性能的 PHP 框架,专注于快速开发和企业级应用
组件模块 | 功能描述 | 访问方式 |
---|---|---|
🧭 路由系统 | 基于 SlimPHP 的路由管理,支持属性注解和传统定义 | App::route() |
💾 数据库层 | 基于 Eloquent ORM 的数据库操作,支持多数据库 | App::db() |
🗄️ 缓存系统 | 多驱动缓存系统,支持 Redis、文件、内存等 | App::cache() |
📋 队列系统 | 异步任务处理,支持数据库、Redis 等驱动 | App::queue() |
📡 事件系统 | 事件驱动编程,支持同步和异步事件处理 | App::event() |
🔐 认证授权 | 完整的用户认证和权限管理系统 | App::auth() |
💿 存储系统 | 统一的文件存储接口,支持本地和云存储 | App::storage() |
📊 日志系统 | 基于 Monolog 的日志记录系统 | App::log() |
# 使用项目模板快速创建新项目
composer create-project duxweb/dux-lite-starter my-app
# 进入项目目录
cd my-app
# 使用 Composer 创建新项目
composer create-project duxweb/dux-lite my-app
# 或者在现有项目中安装
composer require duxweb/dux-lite:^2.0
# 使用 PHP 内置服务器
cd public
php -S localhost:8000
# 访问 http://localhost:8000
<?php
// app/Web/App.php - Web 模块注册类
namespace App\Web;
use Core\App\AppExtend;
use Core\Bootstrap;
class App extends AppExtend
{
public function init(Bootstrap $bootstrap): void
{
// 模块初始化逻辑
}
public function register(Bootstrap $bootstrap): void
{
// 注册服务和组件
}
public function boot(Bootstrap $bootstrap): void
{
// 模块启动逻辑
}
}
<?php
// 传统路由定义
use Core\App;
App::route()->get('/users', [UserController::class, 'index']);
App::route()->post('/users', [UserController::class, 'store']);
App::route()->get('/users/{id}', [UserController::class, 'show']);
// 属性注解路由
#[Route('/api/users', methods: ['GET'])]
#[Route('/api/users', methods: ['POST'], name: 'users.store')]
class UserController
{
public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$users = User::paginate(15);
return response()->json($users);
}
public function store(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$data = $request->getParsedBody();
$user = User::create($data);
return response()->json($user, 201);
}
}
<?php
// 查询构造器
$users = App::db()->table('users')
->where('status', 1)
->orderBy('created_at', 'desc')
->paginate(15);
// Eloquent 模型
class User extends Model
{
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password'];
public function posts()
{
return $this->hasMany(Post::class);
}
}
// 模型操作
$user = User::create([
'name' => '张三',
'email' => 'zhang@example.com',
'password' => password_hash('123456', PASSWORD_DEFAULT)
]);
$users = User::with('posts')->where('status', 1)->get();
<?php
// 基础缓存操作
$cache = App::cache();
// 设置缓存
$cache->set('user:1', $userData, 3600);
// 获取缓存
$userData = $cache->get('user:1');
// 缓存闭包
$users = $cache->remember('users:active', 3600, function() {
return User::where('status', 1)->get();
});
// Redis 缓存
$redis = App::cache('redis');
$redis->set('session:' . $sessionId, $sessionData, 1800);
<?php
// 定义队列任务
namespace App\Common\Jobs;
use Core\Queue\QueueMessage;
class SendEmailJob extends QueueMessage
{
public function __construct(
private string $to,
private string $subject,
private string $content
) {}
public function handle(): void
{
// 发送邮件逻辑
$mailer = App::di()->get('mailer');
$mailer->send($this->to, $this->subject, $this->content);
}
public function failed(\Throwable $exception): void
{
// 任务失败处理
App::log()->error('邮件发送失败', [
'to' => $this->to,
'error' => $exception->getMessage()
]);
}
}
// 分发任务
App::queue()->push(new SendEmailJob(
'user@example.com',
'欢迎注册',
'欢迎使用 DuxLite 框架!'
));
// 延迟分发
App::queue()->later(300, new SendEmailJob(...));
<?php
// 定义事件监听器
#[Listener('user.created')]
class UserCreatedListener
{
public function handle($event): void
{
$user = $event['user'];
// 发送欢迎邮件
App::queue()->push(new SendWelcomeEmailJob($user->email));
// 记录日志
App::log()->info('新用户注册', ['user_id' => $user->id]);
}
}
// 触发事件
App::event()->dispatch('user.created', ['user' => $user]);
DuxLite 提供了强大的命令行工具来提升开发效率:
# 查看所有可用命令
php dux
# 数据库相关命令
php dux db:sync # 同步数据库结构
php dux db:list # 查看数据库列表
php dux db:backup # 备份数据库
php dux db:restore # 恢复数据库
# 队列处理命令
php dux queue:work # 处理队列任务
php dux queue:work --queue=emails # 处理指定队列
# 路由管理命令
php dux route:list # 查看所有路由
# 权限管理命令
php dux permission:sync # 同步权限数据
# 计划任务命令
php dux schedule:run # 运行计划任务
推荐使用 FlyEnv[1] 作为本地开发环境:
public
目录Docker 部署(推荐)
# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "80:80"
volumes:
- ./data:/var/www/html/data
environment:
- APP_ENV=production
depends_on:
- db
- redis
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
volumes:
- mysql_data:/var/lib/mysql
redis:
image: redis:7-alpine
volumes:
- redis_data:/data
volumes:
mysql_data:
redis_data:
宝塔面板部署
public
详细部署说明请参考:部署指南[2]
项目类型 | 描述 | 链接 |
---|---|---|
基础应用 | 展示框架基本功能的示例项目 | 查看示例[8] |
API 应用 | RESTful API 开发示例 | 查看示例[9] |
企业应用 | 完整的企业级应用示例 | 查看示例[10] |
[1]
FlyEnv: https://flyenv.com/
[2]
部署指南: https://duxweb.github.io/dux-lite/guide/deployment.html
[3]
完整文档: https://duxweb.github.io/dux-lite/
[4]
快速入门: https://duxweb.github.io/dux-lite/guide/getting-started.html
[5]
架构设计: https://duxweb.github.io/dux-lite/guide/overview.html
[6]
API 参考: https://duxweb.github.io/dux-lite/api/
[7]
最佳实践: https://duxweb.github.io/dux-lite/guide/best-practices.html
[8]
查看示例: https://github.com/duxweb/dux-lite-example
[9]
查看示例: https://github.com/duxweb/dux-lite-api-example
[10]
查看示例: https://github.com/duxweb/dux-lite-enterprise-example