simplemvc/framework
Composer 安装命令:
composer require simplemvc/framework
包简介
SimpleMVC framework
关键字:
README 文档
README
SimpleMVC is an MVC framework for PHP based on the KISS principle:
"Keep It Simple, Stupid"
The goal of this project is to offer a simple to use and fast framework for PHP applications using PSR standards.
SimpleMVC uses the dependency injection pattern to manage the dependencies between classes and the FastRoute library for implementing the routing system.
SimpleMVC uses the following PSR standards, from the PHP-FIG initiative:
This project was born as educational library for the course PHP Programming by Enrico Zimuel at ITS ICT Piemonte in Italy.
Since than, the project has been evoluted and used also for building web application in production. We decided to create a more general purpose project and this was the beginning of this repository.
Introduction
SimpleMVC implements the Model–View–Controller (MVC) architectural pattern using Dependency injection and PSR standards.
A SimpleMVC application looks as follows:
chdir(dirname(__DIR__)); require 'vendor/autoload.php'; use DI\ContainerBuilder; use SimpleMVC\App; use SimpleMVC\Emitter\SapiEmitter; $builder = new ContainerBuilder(); $builder->addDefinitions('config/container.php'); $container = $builder->build(); $app = new App($container); $app->bootstrap(); // optional $response = $app->dispatch(); // PSR-7 response SapiEmitter::emit($response);
In this example we use a DI container with PHP-DI. We create a SimpleMVC\App object
using the previous container.
The application can be configured using the config/container.php file. An example is as follows:
// config/container.php use App\Controller; return [ 'config' => [ 'routing' => [ 'routes' => [ [ 'GET', '/', Controller\HomePage::class ] ] ] ] ];
The steps to manage the request are:
bootstrap()(optional), here you can specify any bootstrap requirements;dispatch(), where the HTTP request is dispatched, executing the controller specified in the route.
The dispatch() returns a PSR-7 response. Finally, we can render the response using an emitter.
In this example we used a SapiEmitter to render the PSR-7 response in the standard output.
The previous PHP script is basically a front controller of an MVC application (see diagram below).
In this diagram the front controller is stored in a public/index.php file.
The public folder is usually the document root of a web server.
Using a pipeline of controllers
If you want you can specify a pipeline of controllers to be executed for a specific route. For instance, imagine to have a route as follows:
// config/container.php use App\Controller; use SimpleMVC\Controller\BasicAuth; return [ 'config' => [ 'routing' => [ 'routes' => [ [ 'GET', '/admin', [BasicAuth::class, Controller\HomePage::class ] ] ], 'authentication' => [ 'username' => 'admin', 'password' => '1234567890' ] ] ];
The route GET /admin will execute the BasicAuth controller first and, if the
authentication will be successfull, the HomePage controller after.
This is a pipeline of two controllers executed in order. The BasicAuth is a
simple implementation of the Basic Access Authentication. This controller uses the username and password
configuration in the authentication section.
If the authentication is not success, the BasicAuth emits an HaltResponse that
will stop the pipeline execution. HaltResponse is a special PSR-7 that informs
the SimpleMVC framework to halt the execution.
Passing attributes between controllers
If you need to pass an attribute (parameter) from a controller to another in a
pipeline of execution you can use the AttributeInterface. For instance, imagine
to pass a foo attribute from a controller A to controller B, using the follwing
routing pipeline:
// config/container.php use App\Controller; return [ 'config' => [ 'routing' => [ 'routes' => [ [ 'GET', '/', [Controller\A::class, Controller\B::class ] ] ] ] ];
You need to create the controller A as follows:
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use SimpleMVC\Controller\AttributeInterface; use SimpleMVC\Controller\AttributeTrait; use SimpleMVC\Controller\ControllerInterface; class A implements ControllerInterface, AttributeInterface { use AttributeTrait; public function execute( ServerRequestInterface $request, ResponseInterface $response ): ResponseInterface { $this->addRequestAttribute('foo', 'bar'); return $response; } }
We can use an AttributeTrait that implements the AttributeInterface with
the addRequestAttribute(string $name, $value). This function adds a PSR-7
attribute into the $request for the next controller.
In order to get the foo parameter in the B controller you can use the
PSR-7 standard function getAttribute() from the HTTP request, as follows:
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use SimpleMVC\Controller\ControllerInterface; class B implements ControllerInterface { public function execute( ServerRequestInterface $request, ResponseInterface $response ): ResponseInterface { $attribute = $request->getAttribute('foo'); pritnf("Attribute is: %s", $attribute); return $response; } }
Notice that you don't need to implement the AttributeInterface for the B controller
since we only need to read from the $request.
Quickstart
You can start using the framework with the skeleton application.
Copyright
The author of this software is Enrico Zimuel and other contributors.
This software is released under the MIT License.
simplemvc/framework 适用场景与选型建议
simplemvc/framework 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 656 次下载、GitHub Stars 达 34, 最近一次更新时间为 2022 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「mvc」 「Simple」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simplemvc/framework 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simplemvc/framework 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simplemvc/framework 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Kinikit - PHP Application development framework MVC component
A simple library for make Lexer and Parsers to build a language
A simple library that allows transform any kind of data to native php data or whatever
A simple library that uses the hydration pattern to fill objects given different sources of data.
Micro Active Record library in PHP, support chain calls, events, and relations.
统计信息
- 总下载量: 656
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 34
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-25
