pfcode / megumin-framework
一个简单的MVC PHP框架
1.4.0
2023-03-02 20:13 UTC
README
这是一个用PHP编写的简单MVC框架。名字灵感来源于动画《Kono Subarashii Sekai ni Shukufuku wo!》中的Megumin:()
概述
此项目的主要目的是通过一组基类和工具,使您能够轻松地开始一个新的项目代码库,这些工具旨在提高您的工作效率并保持代码整洁。
尽管主流的PHP框架提供了很多组件,但这个框架只提供了最常用的组件,例如
- Dispatcher - 执行路由到控制器(支持友好的URL)
- ModelFactory - 通过查询(SQL数据库)操作模型存储
- Model - 代表项目中的一些数据,例如UserModel、PostModel等..
- Controller - 处理输入数据,操作模型并向用户呈现结果
- View - 在页面上呈现独立变量作用域中的数据
还有一些类的基本实现
- MySQLModelFactory - 处理自定义MySQL查询以检索和操作数据的ModelFactory
- ScopeDebugView - 显示有关控制器信息的视图 - 可用于调试
安装
您只需将新的依赖项添加到您的 composer.json
文件中
"require": {
"pfcode/megumin-framework": ">=1.0.0"
},
...并更新您的项目(Composer应自动下载最新包)
composer update
快速开始
在examples
目录中有一个使用MeguminFramework并执行示例路由的index.php
示例。以下是它的基本部分(假设已定义并自动加载了示例控制器)
<?php
// Use MeguminFramework namespace to gain direct access to its components
use pfcode\MeguminFramework\Dispatcher;
use pfcode\MeguminFramework\View;
// Use PSR-4 autoloader generated by composer
require __DIR__ . "/../vendor/autoload.php";
// Set global View parameters (page title doesn't have to be honored by chosen View)
View::setPageTitlePrefix("Website Title");
View::setPageTitleSeparator(" : ");
// Specify namespace that your controllers belongs to
Dispatcher::setControllersNamespace(__NAMESPACE__);
// Set controllers that should be called when routing fails
Dispatcher::setFallbackControllers(DashboardController::class, NotFoundController::class);
// Specify routing table for the dispatcher
// (you can omit this call when you don't want to use friendly URLs)
Dispatcher::setUrlPatterns(array(
"/test/{id}" => TestController::class,
"/test" => TestController::class,
"/" => DashboardController::class
));
// Perform routing and execute proper controller
Dispatcher::explosion();