fabiodoppio / mvc
简单Web应用的Model View Controller (MVC) 设计模式。
2.31
2024-09-28 14:16 UTC
Requires
- php: >=8.0
- phpmailer/phpmailer: ^6.8.0
README
简单Web应用的Model View Controller (MVC) 设计模式。
特性
-
默认页面:首页、登录、注册、恢复、验证、账户和404错误:通过模板文件 (.tpl) 支持简单的自定义,类似于Smarty。
-
缓存引擎:页面自动缓存以提高性能,在适当的时候通过提供缓存内容来减少服务器负载。
-
用户角色:支持用户角色的实现,定义和管理不同的访问级别和权限。
-
账户恢复:用户可以通过友好的恢复流程恢复账户,在忘记密码或其他问题时可以重新获得账户访问权限。
-
账户验证:包含一个内置功能通过电子邮件验证账户,增强了用户注册的安全性和可信度。
-
安全机制:该包实现了现代安全措施以保护免受潜在攻击。这包括对重复不正确或未经授权的输入的冷却期以及验证操作令牌以防止恶意操作。
-
多语言支持
-
两步验证
-
更多功能即将推出..
预览
安装
官方安装方法是通过composer及其Packagist包 fabiodoppio/mvc。
$ composer require fabiodoppio/mvc
..或者只需复制 example 目录并运行
$ composer update
数据库的SQL语句
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; CREATE TABLE `app_accounts`( `id` int UNSIGNED NOT NULL, `username` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `email` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `token` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `role` int UNSIGNED NOT NULL, `registered` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `lastaction` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; INSERT INTO `app_accounts` (`id`, `username`, `email`, `password`, `token`, `role`, `registered`, `lastaction`) VALUES (1000, 'admin', 'someone@example.com', '$2y$10$mF/1IeSTLohx/J35LYnEoueV50p3g9EOgnfADE0E7seJw127fHzY2', 'deP5E5KznHsLl0TMeLyvbndNg7KEky6W', 8, '2023-11-29 00:00:00', '2023-11-29 00:00:00'); CREATE TABLE `app_accounts_meta` ( `id` int UNSIGNED NOT NULL, `name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `app_accounts_log`( `id` int UNSIGNED NOT NULL, `event` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; ALTER TABLE `app_accounts` ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `username` (`username`), ADD UNIQUE KEY `email` (`email`); ALTER TABLE `app_accounts_meta` ADD PRIMARY KEY (`id`,`name`); ALTER TABLE `app_accounts_log` ADD PRIMARY KEY(`id`,`event`,`timestamp`); ALTER TABLE `app_accounts` MODIFY `id` int UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1001; ALTER TABLE `app_accounts_meta` ADD CONSTRAINT `app_accounts_meta_ibfk_1` FOREIGN KEY (`id`) REFERENCES `app_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; ALTER TABLE `app_accounts_log` ADD CONSTRAINT `app_accounts_log_ibfk_1` FOREIGN KEY(`id`) REFERENCES `app_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; COMMIT;
您的 .htaccess 应该看起来像这样
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,QSA,L]
</IfModule>
用法
创建应用的简单用法如下在你的 index.php
<?php require_once __DIR__.'/app/vendor/autoload.php'; MVC\App::config([ "APP_URL" => "https://", // [REQUIRED] url to your app, no trailing slash #"APP_NAME" => "My App", // [OPTIONAL] name of your app #"APP_TITLE" => "", // [OPTIONAL] title of your start page #"APP_AUTHOR" => "", // [OPTIONAL] author of your app #"APP_DESCRIPTION" => "", // [OPTIONAL] description of your app #"APP_LANGUAGE" => "en_EN.utf8", // [OPTIONAL] (server-)language of your app #"APP_LANGUAGES" => [], // [OPTIONAL] available (server-)languages #"APP_CRON" => false, // [OPTIONAL] de/activates cronjob #"APP_LOGIN" => true, // [OPTIONAL] de/activates login (except admins) #"APP_SIGNUP" => true, // [OPTIONAL] de/activates signup #"APP_MAINTENANCE" => false, // [OPTIONAL] de/activates maintenance mode (except admins) #"APP_BADWORDS" => [], // [OPTIONAL] forbidden words for usernames or messages "SALT_COOKIE" => "", // [REQUIRED] randomized hash for security reasons "SALT_TOKEN" => "", // [REQUIRED] randomized hash for security reasons "SALT_CACHE" => "", // [REQUIRED] randomized hash for security reasons "DB_HOST" => "", // [OPTIONAL] hostname to your mysql server "DB_USERNAME" => "", // [OPTIONAL] username to your mysql server "DB_PASSWORD" => "", // [OPTIONAL] password to your mysql server "DB_DATABASE" => "", // [OPTIONAL] database to your mysql server "MAIL_HOST" => "", // [OPTIONAL] hostname to your mail server "MAIL_SENDER" => "", // [OPTIONAL] sender email address for system emails "MAIL_RECEIVER" => "", // [OPTIONAL] receiver email address for contact form "MAIL_USERNAME" => "", // [OPTIONAL] username to your mail server "MAIL_PASSWORD" => "", // [OPTIONAL] password to your mail server #"MAIL_ENCRYPT" => "ssl", // [OPTIONAL] ssl or tsl for encryption #"MAIL_PORT" => "465, // [OPTIONAL] port to your mail server "DIR_ROOT" => "/var/www" // [REQUIRED] path to your root directory, no trailing slash #"DIR_CLASSES" => "/app/classes", // [OPTIONAL] path to your custom or extended classes #"DIR_ASSETS" => "/app/assets", // [OPTIONAL] path to your assets #"DIR_FONTS" => "/app/assets/fonts", // [OPTIONAL] path to your fonts #"DIR_SCRIPTS" => "/app/assets/scripts", // [OPTIONAL] path to your .js scripts #"DIR_STYLES" => "/app/assets/styles", // [OPTIONAL] path to your .css styles #"DIR_LOCALE" => "/app/locale", // [OPTIONAL] path to your locale .mo/.po files #"DIR_VENDOR" => "/app/vendor", // [OPTIONAL] path to your third-party libraries #"DIR_VIEWS" => "/app/views", // [OPTIONAL] path to your template files #"DIR_CACHE" => "/app/cache", // [OPTIONAL] path to your cache files #"DIR_MEDIA" => "/app/media" // [OPTIONAL] path to your media files ]); MVC\App::init(); ?>
您现在可以在此处登录 https://yourdomain/login
用户名: admin 密码: admin123
别忘了更改您的用户名和密码!
调试模式
index.php:
MVC\App::debug();
添加页面
index.php:
MVC\App::page([ "slug" => "/imprint", // [REQUIRED] regular expression of your page slug "title" => "Imprint", // [OPTIONAL] title of your custom page "description" => "This is a custom page", // [OPTIONAL] description of your custom page "robots" => "noindex, nofollow", // [OPTIONAL] robots meta of your custom page "canonical" => "/imprint", // [OPTIONAL] canoncial meta of your custom page "class" => "page imprint", // [OPTIONAL] body class of your custom page "template" => "/imprint.tpl", // [REQUIRED] template file of your custom page "ignore_maintenance" => false // [OPTIONAL] ignore maintenance mode if active ]);
处理模板
您可以通过将模板放入您的 views 目录来覆盖模板。在模板文件中,您可以使用简单的Smarty代码。例如,要包含一个文件
{% include /_includes/mytemplate.tpl %}
..或者显示一个变量
{{$myvar}}
也可以使用类似这样的PHP代码
{% myfunction(); %}
..或者这样
{% if (Condition): %} My Text {% endif; %}
如果您想输出已翻译的文本,您可以这样写您的文本
{{"My text"}}
或者
{{"My %s text", $myvar}}
(但别忘了更新您的 locale 目录中的语言文件!)
详细文档即将推出..