LeafPHPx 是一个非轻量级、模块化的 PHP 框架,专为现代 Web 开发设计。它基于 PSR 标准构建,支持快速路由、依赖注入、缓存、数据库和视图引擎等功能。框架简洁高效,适合 API 开发和简单 Web 应用。
-
克隆项目:
git clone https://github.com/JaneDevStudio/LeafPHPx.git cd LeafPHPx
-
安装依赖:
composer install
-
复制环境文件:
cp .env.example .env
-
配置数据库(可选): 编辑
.env
文件,设置数据库连接:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db DB_USERNAME=root DB_PASSWORD=your_password
-
启动开发服务器:
php bin/leaf serve
访问
http://localhost:8000
,您将看到欢迎页面。
使用属性路由(推荐):
在 app/Controllers/ExampleController.php
中:
<?php
namespace App\Controllers;
use Leaf\Attributes\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class ExampleController
{
#[Route('/hello/{name}', methods: ['GET'])]
public function hello(ServerRequestInterface $request, ResponseInterface $response, string $name): ResponseInterface
{
return $response->json(['message' => "Hello, $name!"]);
}
}
框架会自动扫描 app/Controllers/
目录加载路由。
手动路由(在 config/routes.php
中):
$router->get('/manual', function() { return 'Manual Route'; });
创建模型 app/Models/User.php
:
<?php
namespace App\Models;
use Leaf\Database\Model;
class User extends Model
{
protected static string $table = 'users';
}
使用:
$user = User::find(1);
$users = User::all();
创建视图 resources/views/hello.php
:
<h1>Hello, <?= $name ?>!</h1>
在控制器中:
return $response->view('hello', ['name' => $name]);
创建自定义中间件 app/Middleware/AuthMiddleware.php
(示例见代码)。
在 src/bootstrap.php
中注册:
$app->middlewareStack->add(new \App\Middleware\AuthMiddleware());
使用 CLI 生成控制器:
php bin/leaf make:controller UserController
运行迁移(占位):
php bin/leaf migrate
配置位于 config/
目录:
app.php
:环境、调试、模块开关。database.php
:数据库连接。cache.php
:缓存驱动。logging.php
:日志路径。
支持 YAML/JSON 配置格式。
- API 响应:
$response->json(['data' => $data]);
- 重定向:
$response->redirect('/home', 302);
- 验证:使用
$validator = app()->validator; $validator->validate($data, $rules);
完整示例见 app/Controllers/ExampleController.php
。
运行单元测试:
composer test # 或 phpunit
欢迎贡献!请阅读 Dev.md 获取开发指南。
MIT License. 详见 LICENSE 文件。
感谢 nikic/fast-route、Monolog 等开源项目。