bayrameker / my-bolt-framework
带有 Bolt CLI 的现代 PHP 框架
v0.0.4
2024-07-13 18:17 UTC
Requires
- php: ^7.4 || ^8.0
- doctrine/annotations: ^2.0
- guzzlehttp/guzzle: ^7.0
- illuminate/database: ^8.0
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-29 02:07:39 UTC
README
Bolt 是一个快速轻量级的 PHP 框架。它简单灵活的结构使其非常适合小型到中型项目。
特性
- MVC (模型-视图-控制器) 架构
- 简单的路由系统
- 易于快速依赖注入
- 视图渲染支持
安装
- 使用 Composer 创建新项目
composer create-project bayrameker/my-bolt-framework new-project
cd new-project
- 安装必要的依赖
composer install
- 创建
.env
文件并配置必要的设置
cp .env.example .env
- 启动服务器
php bolt serve
使用方法
Bolt 命令
Bolt 框架提供的命令有:
php bolt migrate
- 执行数据库迁移。php bolt create:migration {name}
- 创建新的数据库迁移。php bolt seed
- 执行数据库种子。php bolt controller {name} [-v]
- 创建新的控制器。使用-v
选项还可以添加视图和路由。php bolt model {name}
- 创建新的模型。php bolt service {name}
- 创建新的服务。php bolt repository {name}
- 创建新的仓库。php bolt dump-autoload
- 更新 Composer 自动加载文件。php bolt serve
- 启动应用程序。
路由器
您可以在 routes/web.php
文件中添加新的路由
$router->get('/home', [App\Controllers\HomeController::class, 'index']);
控制器
要创建新的控制器,在 app/Controllers
目录中创建新的 PHP 文件
<?php namespace App\Controllers; use Core\Controller; use Core\Request; use Core\Response; use Core\ViewRenderer; use App\Services\HomeService; class HomeController extends Controller { protected $homeService; public function __construct(HomeService $homeService) { $this->homeService = $homeService; } public function index(Request $request, Response $response) { $homeData = $this->homeService->getHomeData(); $viewRenderer = new ViewRenderer('home/index', [ 'title' => $homeData->title, 'message' => $homeData->message, 'layout' => 'layout' ]); $viewRenderer->render(); } }
服务
服务控制业务逻辑。在 app/Services
目录中创建新的 PHP 文件
<?php namespace App\Services; use App\Repositories\HomeRepository; class HomeService { protected $homeRepository; public function __construct(HomeRepository $homeRepository) { $this->homeRepository = $homeRepository; } public function getHomeData() { return $this->homeRepository->getHomeData(); } }
仓库
仓库控制数据访问层。在 app/Repositories
目录中创建新的 PHP 文件
<?php namespace App\Repositories; use App\Models\Home; class HomeRepository { public function getHomeData() { return new Home('Home Page', 'Welcome to My Bolt Framework!'); } }
模型
模型表示数据结构。在 app/Models
目录中创建新的 PHP 文件
<?php namespace App\Models; class Home { public $title; public $message; public function __construct($title, $message) { $this->title = $title; $this->message = $message; } }
视图
视图文件控制用户显示的 HTML 内容。在 app/Views
目录中创建新的 PHP 文件
app/Views/home/index.php
<main> <h1><?= $title ?? 'Default Title' ?></h1> <p><?= $message ?? 'Default Message' ?></p> </main>
app/Views/layout.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?= $title ?? 'Default Title' ?></title> </head> <body> <?= $content ?> </body> </html>
贡献
我们欢迎贡献!请首先打开一个 issue 来讨论您希望做出的任何更改。
- 分叉仓库
- 创建新的分支 (
git checkout -b feature/AmazingFeature
) - 提交您的更改 (
git commit -m '添加一些 AmazingFeature'
) - 将更改推送到分支 (
git push origin feature/AmazingFeature
) - 打开 Pull Request
许可证
此项目采用 MIT 许可证。有关更多信息,请参阅 LICENSE
文件。