filrichard / mmf_skeleton
MVC 微型框架的完整实现
03
2019-08-03 07:05 UTC
Requires
This package is not auto-updated.
Last update: 2024-10-01 05:04:35 UTC
README
MVC 微型框架的完整实现
MMF_skeleton 是一个项目,展示了如何实现 mvc_micro_framework。
mvc_micro_framework 是一个简单轻量级的 PHP 微型框架,允许您轻松快速地构建 MVC 应用程序。它简单且可扩展!
最小要求
mvc_micro_framework 需要 PHP 7.1
或更高版本。
安装
- 下载
使用 Composer
composer require filarichard/mvc_micro_framework
不使用 Composer
下载文件并将其直接解压到网站目录中。
- 创建 App 并调用 run 函数
$application = new \App\App(); $application->run();
- 在
/config
目录中创建 config.json 文件
{ "routes": { "/third_parties_components": { "controller": "Controllers\\ThirdPartiesController", "action": "tpc" } }, "modules": [ "mvc" ], "diContainer": "config/diContainer.php" }
可能的配置值包括
- routes - 应用中使用的所有路由列表,不使用模块。
- modules - 应用中使用的所有模块列表。
- router - 配置的路由的 PHP 路径。
- diContainer - 配置的 DI 容器的 PHP 路径。
- request - 配置的 HTTP 请求的 PHP 路径。
路由
为了完全工作的路由,请使用以下目录
- config
- src
- config
- 所有模块的配置文件
- modelname.config.json
- controllers
- 所有控制器
- 如果已定义,使用模块名称
- 模块控制器
- models
- views
- config
对于路由设置,您可以选择以下之一
- 在 config 文件中定义路由
不在模块中包含的所有路由创建在 config.json 文件中。
在模块中定义的所有路由创建在 modulename.config.json 文件中。
路由定义的结构
"/nameOfRoute": { "controller": "Controllers\\NameOfController", "action": "nameofFunction" }
- 定义包含所有路由的自定义 router
$router = new \MVC\Router(); $router->addRoute(new \MVC\Route("/nameOfRoute", \Controllers\NameOfController::class, "nameOfFunction")); return $router;
模板引擎
mvc_micro_framework 包含内置的模板引擎。只需遵循以下几个步骤
- 设置模板
$this->setView();
- 分配变量
$this->view->assignValue("nameOfVariable", $value); $this->view->assignValue("title1", $titleValue); $this->view->assignValue("subtitle1", $subtitleValue); $this->view->assignValue("content1", $contentValue);
- 在 HTML 代码中使用这些变量
<h1>{title1}</h1> <h2>{subtitle1}</h2> <p> {content1} </p>
- 渲染视图
$this->view->render("pathToFile.html");
函数 render 可以接受第二个参数,用于定义路径是完整路径还是相对于 /src/views
的相对路径。
事件调度器
通过以下步骤,您可以使用我们的事件调度器
- 创建新的调度器
$this->eventDispatcher = new Dispatcher();
- 创建新的事件
$this->eventName = new Event("eventName");
- 将其附加到监听器
$this->eventDispatcher->attach($this->eventName, array($this, 'listenerName'));
注意:监听器可以是任何可调用对象。
- 调度事件
$this->eventDispatcher->dispatch($this->eventName);
您还可以删除事件
$this->eventDispatcher->detach($this->eventName, array($this, 'listenerName'));
或停止事件传播
$this->eventName->setPropagationStopped(true);
SQL
-- phpMyAdmin SQL Dump -- version 4.8.5 -- https://www.phpmyadmin.net/ -- -- Počítač: 127.0.0.1 -- Vytvořeno: Čtv 22. srp 2019, 11:18 -- Verze serveru: 10.1.38-MariaDB -- Verze PHP: 7.3.2 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Databáze: `test` -- -- -------------------------------------------------------- -- -- Struktura tabulky `segments` -- CREATE TABLE `segments` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `title` varchar(255) DEFAULT NULL, `subtitle` varchar(255) DEFAULT NULL, `content` varchar(3000) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Vypisuji data pro tabulku `segments` -- INSERT INTO `segments` (`id`, `name`, `title`, `subtitle`, `content`) VALUES (2, 'template1', 'Template', 'MVC View', '<b>View</b> functionality in our MVC Micro Framework is backed by the <b>Template</b> class (Template View design pattern). When creating a view, the variable name will be placed in curly brackets where we want to display values from the logical part of the application. For example <span class=\"blue\">{value}</span>. These values must be correctly assigned in the controller.'), (3, 'template2', NULL, 'Inserting values', 'Each segment on these example pages contains a paragraph or a first and second order heading. A template system is used on this page replacing the Template tags with associated values.'); -- -- Klíče pro exportované tabulky -- -- -- Klíče pro tabulku `segments` -- ALTER TABLE `segments` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT pro tabulky -- -- -- AUTO_INCREMENT pro tabulku `segments` -- ALTER TABLE `segments` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
第三方组件
mvc_micro_framework 支持以下第三方组件
- HTTP Foundation/Symfony
- Eloquent (ORM)/Laravel
- Pimple (DI 容器)
- Monolog (日志记录器)
Packagist
https://packagist.org.cn/users/richard.fila/packages/
许可证
Flight 在 MIT 许可证下发布。