sigawa / mvc-core
为开发目的定制的 MVC 小型框架,并提供改进空间
Requires
- phpmailer/phpmailer: dev-master
- symfony/console: 7.1.x-dev
- vlucas/phpdotenv: 5.6.x-dev
README
介绍
欢迎来到我的自定义 MVC 框架!这个框架受 Laravel、symfony 和 Codeignitor 路由功能的深入了解启发,旨在轻量级、灵活且易于使用。它非常适合希望了解 MVC 框架内部工作原理的开发者,或者那些想要构建小型到中型应用程序而不希望承受完整框架的负担的开发者。
安装
您可以通过 Composer 安装此框架
composer require sigawa/mvc-core:^1.0.1
功能
- 轻量级且快速:最小化开销,并针对性能进行了优化。
- MVC 结构:通过模型、视图和控制器实现干净的职责分离。
- 路由:简单而强大的路由系统。
- 数据库集成:易于使用的数据库抽象层。
- CRUD 类已就绪:所有 CRUD 操作都方便且易于自定义。
- 命令行工具 'mcconsole':使用 'mcconsole' 命令工具类轻松运行常用命令(创建、生成等)。
- 模板化:基本的模板化引擎,用于动态视图。
- 错误处理:友好的错误页面和详细的堆栈跟踪。
要求
- PHP 7.4 或更高版本
- Composer
入门
1. 安装
在克隆存储库之前,请确保已初始化环境。例如,运行
composer init
然后
克隆存储库或通过 Composer 安装
composer require sigawa/mvc-core:^1.0.1 //OR git clone https://github.com/Jos254Kenya/k-mvc-core.git
将 mcconsole 文件复制到您的项目根目录中,以便在终端中访问它。
您可以使用以下命令初始化新项目
php mcconsole create:project # next enter your project name # it will automatically create the project structure with initial file
2. 配置
安装后,通过复制示例环境文件并更新它来配置您的应用程序
cp .env.example .env
如果希望用于 CLI,请将 mcconsole 文件复制到项目根目录中
2.1. 用户类
运行以下命令以创建用户模型
php mcconsole make:model User
将函数/类体替换为 User.example 文件中的内容。确保您的类包含
use sigawa\mvccore\UserModel;
2.2. index.php
将 index.example 的内容复制到 /app/public/index.php
。
php mcconsole make:model User
然后您需要将文件中的函数/类体替换为 'User.example' 文件中的内容。该文件提供了一个典型模型类的结构。您可以按需修改它,但请记住始终在您的类中包含以下行
use sigawa\mvccore\UserModel;
3. 运行应用程序
使用内置的 PHP 服务器或 mcconsole serve 命令来运行您的应用程序
php -S localhost:8000 -t public
或者
php mcconsole serve
在浏览器中导航到 https://:8000
,以查看应用程序的实际效果。
文档
路由
在 public/index.php
文件中定义您的路由
$app->router->get('/', [HomeController::class,'index']); $router->post('/submit', [HomeController::class,'functioname']); //$router->get('/get/${id}/',[HomeController::class,'functioname']);
控制器
在 app/Controllers
目录中使用以下方法创建控制器
php mcconsole make:controller Controllername
示例控制器
namespace App\Controllers; use sigawa\mvccore\Request; use sigawa\mvccore\Response; use sigawa\mvccore\Controller; class ControllernameController extends Controller { public function index(Request $request, Response $response) { $this->setLayout('layoutname'); // return $this->render($view, $params = [] optional, $layoutDirectory = '' optional); // by default, your layouts will be in the App/views/layout return $this->render('home'); } }
模型
在 app/Models
目录中使用以下方法创建模型
php mcconsole make:model Modelname
示例模型
namespace App\Models; use sigawa\mvccore\db\DbModel; class permission extends DbModel { public string $PermissionName =''; public string $Description =''; public static function tableName(): string { return 'permission'; } public function attributes(): array { return ['PermissionName','Description']; } public function rules() { return [ 'attributename'=>[self::RULE_REQUIRED], // other attributes and rules, explore the multiple rules in the Base method ]; } public function save() { return parent::save(); } }
CRUD
在 CRUD 类中访问所有 CRUD 函数。示例
namespace namespace\Controllers; use Sigawa\Hcp\models\permission; use sigawa\mvccore\Application; use sigawa\mvccore\db\CRUD; use sigawa\mvccore\Request; use sigawa\mvccore\Controller; class PermissionsController extends Controller { private $crud; private $permision; public function __construct(){ $this->crud =new CRUD(Application::$app->db); $this->permision =new permission(); } public function index() { // Logic for your index method goes here $this->setLayout('authenticated'); return $this->render('permissions'); } public function loadpermission(Request $request) { if($request->getMethod()==='get'){ $data =$this->crud->getAll('permission','*',[]); echo json_encode($data); } } public function update(Request $request) { if($request->getMethod()==='post') { $input = json_decode(file_get_contents('php://input'), true); $description = $input['Description']; $id =$input['id']; $data =['Description' =>$description]; $condition= ['id'=>$id]; $updateResult =$this->crud->update('permission',$data,$condition); if ($updateResult['success']) { if ($updateResult['changesMade']) { return true; } else { echo json_encode("You did not make any changes"); } } else { echo json_encode('Update Failed. Kindly make sure you typed the right data'); } } } }
视图
在 app/views
目录中创建视图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Home</title> </head> <body> <h1>Welcome to My Custom MVC Framework!</h1> </body> </html>
贡献
欢迎贡献!请随时提交拉取请求或打开一个问题,以帮助改进此项目。
许可证
本项目采用 MIT 许可证。有关详细信息,请参阅 LICENSE 文件。
致谢
- 受 Laravel、symfony 和 codeignitor 路由机制的启发
- 特别感谢所有贡献者
编码愉快!