andreimosman / mvczitto
非常小巧的基于文件系统路由的MVC框架。
Requires
- php: >=7.4.0
README
MVCzitto 是一个 PHP 框架,允许您以简单和便捷的方式构建 Web 应用程序。
不同于传统的基于面向对象 MVC,我们决定使用文件系统路由实现 MVC。
这个想法的背后的目的是尽可能使用纯 PHP 编写代码,但提供一些模式和组织。
有两件事情激励我这样做
- Next.js 路由
- Rasmus Lerdorf 说 "PHP 框架都不好"。我正试图创建这个框架,使其尽可能少地像框架。
如何使用
通过 composer 添加
composer create-project andreimosman/mvczitto foldername
如果您没有 composer,请参阅 Composer 网站,了解如何在您的平台上安装它。或者,您也可以从 发布页面 下载 Frameworkitto,并将其导出到项目的根目录。
Docker 开发环境
在 docker-dev-environment 文件夹中,您可以调用 firstrun.sh 以使用 docker compose 创建开发环境。
如果您还没有 Docker,请参阅 Docker 网站,了解如何在您的平台上安装它。
开始使用 MVCzitto
应用文件夹包含更多说明和一些示例
app
├── index.php (entry point - where de dependency injection is done)
├── config.php (configuration file)
├── assets
│ ├── css
│ │ └── style.css
│ └── images
│ └── logo-mvczitto.png
├── controllers
│ ├── authenticated (controllers that require authentication)
│ │ ├── dashboard
│ │ │ └── index.php
│ │ ├── index.php
│ │ └── user
│ │ ├── @(post)new.php
│ │ ├── edit
│ │ │ ├── @(put,patch)[id].php
│ │ │ └── [id].php
│ │ ├── logout.php
│ │ ├── new.php
│ │ └── profile.php
│ └── open (controllers that don't require authentication)
│ ├── gettingstarted
│ │ └── index.php
│ ├── index.php
│ └── user
│ ├── @(post)forgotpassword.php
│ ├── @(post)login.php
│ ├── @(post)signup.php
│ ├── forgotpassword.php
│ ├── index.php
│ ├── login.php
│ └── signup.php
├── models (filesystem base models)
│ └── users
│ ├── create.php
│ ├── delete.php
│ ├── read.php
│ └── update.php
└── views (follows the same pattern as controllers)
├── authenticated
│ ├── footer.php
│ ├── header.php
│ └── user
│ └── edit
│ └── [id].php
└── open
├── footer.php
├── gettingstarted
│ └── index.php
├── header.php
└── user
├── forgotpassword.php
├── login.php
└── signup.php
路由动词
默认的动词集是 GET,但您可以在文件名开头的 @() 中指定动词,例如 @(post)new.php
身份验证
文件夹 authenticated 和 open 分别表示在用户已认证或未认证的情况下,路由都是有效的。
非常简单的身份验证模式是
$auth = \MVCzitto\Application\Authentication::getInstance()
$somethingNotNull = "WHAT EVER YOU WANT. OBJECTS, ARRAYS, STRINGS";
$auth->setAuthenticationData($somethingNotNull);
这样做 $auth->isAuthenticated() 将返回 true;
您可以通过调用 $auth->unsetAuthenticationData() 来注销
请参阅 app/controllers/open/user/@(post)login.php 和 app/controllers/authenticated/user/logout.php
文件系统模型
├── models
└── users
├── create.php
├── delete.php
├── read.php
└── update.php
您可以通过调用 $models->nameOfTheController 在控制器中访问它们。请参阅 app/controllers/open/user/@(post)login.php
$usersModel = $models->users; // Load the model
$user = $usersModel->read(['email' => $email]); // Find the user
它执行位于 app/models/users/read.php 的代码片段。
主 index.php
app/index.php 包含应用程序的入口点和依赖注入器。
文件系统 CLI 脚本
与 controllers 和 models 一样工作,您可以在 ./app/cli 中创建 CLI 命令。
在应用中添加了一些示例
app/console
└── backup
├── database.help
├── database.php
└── uploaded-files.php
用法
$ ./cli
No command specified.
Usage:
./cli <command> [<parameters>]
Available commands:
backup/database
backup/uploaded-files