带有基本MVC和服务容器支持的微型PHP框架


README

Software License PDS Skeleton

GitHub Build Quality Gate Status Type Coverage Mutation testing badge

此仓库提供了一个带有基本MVC和服务容器支持的微型PHP框架。其名称来源于其目标项目类型 - 小型(几乎)非动态项目,或称惯性项目。

在大型项目中使用完整的框架后,使用纯PHP(通过require)且没有控制器和动作的工作方式可能会显得有些奇怪。主要想法是将控制器和动作引入小型私有项目以组织代码,同时保持纯PHP的配置简单性以及最小化依赖。这就是这个框架的诞生原因。后来,在开发过程中,添加了服务定位器。

最小安装

通过Composer安装Inert

composer require milan-miscevic/inert

index.php

<?php

use Mmm\Inert\ActionContainer;
use Mmm\Inert\Application;
use Mmm\Inert\ServiceContainer;

require '../vendor/autoload.php';

$actions = [
    'index' => IndexAction::class,
];

$actionContainer = new ActionContainer(
    $actions,
    new ServiceContainer([])
);

echo (new Application($actionContainer))->run()->getContent();

IndexAction.php:

<?php

use Mmm\Inert\Action;
use Mmm\Inert\Response;

class IndexAction implements Action
{
    public function run(): Response
    {
        return new Response('Hello, world!');
    }
}