LingDuCodePHP 框架 v2.0 特性文档
🌟 框架核心特色
1. 零依赖设计
无Composer依赖 - 完全独立运行,无需安装任何第三方包
纯PHP实现 - 所有功能均使用原生PHP开发
轻量级 - 核心代码精简,加载快速
2. 企业级数据库支持
多数据库驱动
// MySQL
'default' => 'mysql'
// SQLite
'default' => 'sqlite'
// PostgreSQL
'default' => 'pgsql'
// SQL Server
'default' => 'sqlsrv'
读写分离
'mysql_rw' => [
'read' => [
'host' => '192.168.1.10,192.168.1.11', // 多从库
],
'write' => [
'host' => '192.168.1.1', // 主库
],
]
集群支持
'mysql_cluster' => [
'read' => [
'host' => ['slave1', 'slave2', 'slave3'], // 自动负载均衡
],
]
3. 强大的ORM系统
基础查询
// 查询所有
$users = User::all();
// 条件查询
$users = User::where('status', 1)->where('age', '>', 18)->get();
// 分页
$users = User::paginate(15);
// 聚合函数
$count = User::count();
$sum = Order::sum('amount');
模型关联
class User extends Model
{
// 一对一
public function profile()
{
return $this->hasOne(Profile::class);
}
// 一对多
public function posts()
{
return $this->hasMany(Post::class);
}
// 多对多
public function roles()
{
return $this->belongsToMany(Role::class, 'user_roles', 'user_id', 'role_id');
}
}
软删除
class User extends Model
{
protected $softDelete = true;
protected $deleteTime = 'deletetime';
}
$user->delete(); // 软删除
$user->restore(); // 恢复
$user->forceDelete(); // 强制删除
4. 国际化支持 (i18n)
多语言配置
// 设置语言
Lang::setLocale('zh-cn');
// 获取翻译
trans('common.welcome');
trans('validation.required', ['attribute' => '用户名']);
// 带参数翻译
trans('common.hello', ['name' => '张三']);
// 复数形式
trans_choice('items.count', $count);
语言文件结构
lang/
├── zh-cn/
│ ├── common.php
│ ├── validation.php
│ └── pagination.php
└── en/
├── common.php
├── validation.php
└── pagination.php
5. 任务调度系统
定义调度任务
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
// 每分钟执行
$schedule->command('cache:clear')->everyMinute();
// 每小时执行
$schedule->job(CleanupJob::class)->hourly();
// 每天凌晨执行
$schedule->exec('backup.sh')->dailyAt('02:00');
// 每周执行
$schedule->command('report:weekly')->weekly();
// 工作日执行
$schedule->job(ReportJob::class)->weekdays()->at('09:00');
// 防止重叠执行
$schedule->command('heavy:task')
->everyFiveMinutes()
->withoutOverlapping(30);
}
Cron表达式支持
$schedule->command('task')->cron('*/5 * * * *'); // 每5分钟
$schedule->command('task')->hourlyAt(17); // 每小时17分
$schedule->command('task')->monthlyOn(4, '15:00'); // 每月4号15:00
6. 性能监控工具
调试面板
// 启用调试
Debug::enable();
// 自动显示:
// - 执行时间
// - 内存使用
// - SQL查询
// - 请求数据
// - Session数据
// - 加载文件列表
性能分析
// 基准测试
$result = Debug::benchmark(function() {
// 测试代码
}, 1000);
// 代码分析
$profile = Debug::profile(function() {
return User::with('posts')->get();
}, 'User Query');
7. 增强验证器
验证规则
$validator = Validator::make($data, [
'username' => 'required|alpha_dash|min:3|max:20|unique:users',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:8|confirmed',
'phone' => 'required|phone',
'id_card' => 'required|id_card',
'age' => 'required|integer|between:18,100',
'website' => 'url',
'avatar' => 'image|max:2048',
]);
if ($validator->fails()) {
return $validator->errors();
}
自定义规则
Validator::extend('strong_password', function($field, $value, $params) {
return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/', $value);
});
条件验证
$validator->sometimes('company', 'required', function($data) {
return $data->account_type === 'business';
});
8. 强大的模板引擎
控制结构
{if $user}
欢迎 {$user.name}
{elseif $guest}
请登录
{else}
访客模式
{/if}
{volist name="users" id="user" key="k"}
[tr]
[td]{$k}[/td]
[td]{$user.name}[/td]
[td]{$user.email}[/td]
[/tr]
{/volist}
{foreach $items as $item}
{$item.name}
{/foreach}
{for start="1" end="10" name="i"}
第 {$i} 次
{/for}
{switch $status}
{case 1}启用{/case}
{case 0}禁用{/case}
{default}未知{/default}
{/switch}
过滤器
{$content|safe} XSS过滤
{$title|truncate=50} 截断
{$price|money} 格式化金额
{$date|date='Y-m-d'} 日期格式
{$name|default='匿名'} 默认值
{$text|upper} 大写
{$text|lower} 小写
{$text|strip_tags} 去除HTML
模板继承
{block name="header"}默认头部{/block}
{block name="content"}{/block}
{block name="footer"}默认底部{/block}
{extend name="layout" /}
{block name="content"}
页面内容
{/block}
9. 安全特性
CSRF防护
{:csrf_field()}
XSS过滤
$clean = Security::xssClean($dirty);
SQL注入防护
// 自动参数绑定
User::where('name', $input)->get();
密码加密
$hash = Security::passwordHash('password');
$verified = Security::verifyPassword('password', $hash);
10. 缓存系统
多驱动支持
Cache::store('file')->set('key', $value);
Cache::store('redis')->set('key', $value);
Cache::store('memcache')->set('key', $value);
缓存方法
// 基础操作
Cache::set('key', $value, 3600);
Cache::get('key', $default);
Cache::has('key');
Cache::delete('key');
Cache::clear();
// 记住缓存
$value = Cache::remember('users', 3600, function() {
return User::all();
});
// 自增/自减
Cache::inc('views');
Cache::dec('stock');
// 标签缓存
Cache::tags(['user', 'profile'])->set('key', $value);
📊 与主流框架对比
特性 LingDuCodePHP Laravel ThinkPHP
零依赖 ✅ ❌ ❌
读写分离 ✅ ✅ ✅
多数据库 ✅ ✅ ✅
ORM ✅ ✅ ✅
国际化 ✅ ✅ ✅
任务调度 ✅ ✅ ✅
调试面板 ✅ ✅ ✅
模板引擎 ✅ Blade ✅
安装复杂度 简单 复杂 中等
学习曲线 平缓 陡峭 中等
性能 高 中 中
🚀 快速开始
环境要求
PHP >= 7.4
PDO扩展
Mbstring扩展
安装
# 下载框架
git clone https://github.com/lingdu/LingDuCodePHP.git
# 配置环境
cp .env.example .env
# 设置密钥
# 编辑 .env 文件,设置 APP_KEY
# 导入数据库
mysql -u root -p database < database.sql
目录结构
LingDuCodePHP/
├── app/ # 应用目录
│ ├── admin/ # 后台模块
│ ├── api/ # API模块
│ ├── common/ # 公共模块
│ └── install/ # 安装模块
├── config/ # 配置文件
├── framework/ # 框架核心
│ ├── core/ # 核心类
│ ├── db/ # 数据库
│ ├── cache/ # 缓存
│ ├── template/ # 模板
│ └── ...
├── lang/ # 语言文件
├── public/ # 公共资源
├── runtime/ # 运行时目录
└── lingdu # 命令行工具
📝 版本更新
v2.0.0 新增特性
✅ 数据库读写分离与集群支持
✅ 国际化多语言系统
✅ 任务调度系统
✅ 性能监控调试面板
✅ 增强验证器(30+规则)
✅ 模板引擎增强(20+过滤器)
✅ 多数据库驱动支持
✅ 安全增强(CSRF/XSS/SQL注入)
LingDuCodePHP - 零依赖、高性能、企业级PHP框架
框架开源下载地址:https://gitee.com/lingdubingfeng/ling-du-php-framework

