merophp/framework

又一个为网络应用设计的简单框架 :-)

0.1-beta 2022-03-15 16:27 UTC

This package is auto-updated.

Last update: 2024-09-29 09:02:12 UTC


README

Merophp 是一个为网络应用设计的微型框架。它主要设计为用于重构旧项目的工具。这意味着您可以将其集成,而无需立即进行大规模迁移。相反,您可以在较长时间内进行迁移。

它实现了以下标准

  • psr-4
  • psr-7
  • psr-12
  • psr-15
  • psr-16
  • psr-17

它提供了

  • 一个路由器
  • PSR-4 自动加载器
  • 一些缓存类
  • HTTP 处理
  • 捆绑管理
  • 带有依赖注入的对象管理
  • 一个事件管理器
  • 一个简单的视图引擎

安装

通过 composer

composer require merophp/framework

基本用法

use Merophp\Framework\AppFactory;
use Merophp\Router\Routes\GetRoute;

require_once dirname(__DIR__).'/vendor/autoload.php';

//Create the app object
$app = AppFactory::create();

//Add routes to the router
$app->getRouter()->addRoutes(
    new GetRoute('/hello-world', function($request, $response){
        $response->getBody()->write('Hello World');
        return $response;
    })
);

//Run Forrest, run!
$app->start();

这就是创建一个 hello world 应用程序所需的所有内容。

捆绑概念

您可以使用捆绑(也称为插件)来组织和灵活化您的应用程序。Merophp 提供了一个用于操作和扩展请求处理的捆绑接口。捆绑应作为独立的 composer 包使用,以便使用 composer 的自动加载和依赖管理。您的捆绑必须提供一个启动器类,该类实现了接口 Merophp\BundleManager\BundleBootstrapper\BundleBootstrapperInterface 或扩展了 Merophp\Framework\BundleManagement\BundleBootstrapper\AbstractBundleBootstrapper 类。

{your bundle path}/src/Bootstrapping/Bootstrapper.php

namespace MyVendor\MyBundle\Bootstrapping;

use Merophp\Framework\BundleManagement\BundleBootstrapper\AbstractBundleBootstrapper;
use Merophp\Router\Routes\GetRoute;

class Bootstrapper extends AbstractBundleBootstrapper
{
    public function setup(){
        $this->app->getRouter()->addRoutes(
            new GetRoute('/hello-world', function($request, $response){
                $response->getBody()->write('Hallo World');
                return $response;
            })
        );
    }
    public function tearDown(){}
}

{your bundle path}/composer.json

{
  "name": "my-vendor/my-bundle",
  "autoload": {
    "psr-4": {"MyVendor\\MyBundle\\": "src/"}
  },
  "require": {
	"merophp/framework":"*"
  }
}

运行: composer update

index.php

use Merophp\Framework\AppFactory;

require_once dirname(__DIR__).'/vendor/autoload.php';

//Create the app object
$app = AppFactory::create();

//Register bundle
$app->registerBundle('MyVendor\\MyBundle');

//Run Forrest, run!
$app->start();

您还可以在捆绑启动器的设置方法中注册捆绑。

使用控制器类

在大多数情况下,您不想将回调函数添加到路由中,而是使用带有操作的控制器。Merophp 提供了一个易于使用的控制器集成

添加路由

use MyVendor\MyBundle\Controller\MyCoolController;

$this->app->getRouter()->addRoutes(
     new GetRoute('/hello/{with-name}', [MyCoolController::class, 'myAction'])
);

{your bundle path}/src/Controller/MyCoolController.php

namespace MyVendor\MyBundle\Controller;

use Merophp\Framework\RequestControlling\AbstractController;

class MyCoolController extends AbstractController
{
    /**
     * @param string $name The param from the url
     */
    public function myAction($name)
    {
        //$arguments = $this->request->getParsedBody();

        $this->view->text('Hello '.$name);
    }
}

通过向 https://{your-address}/hello/Martin 发送 HTTP 请求将返回响应 'Hello Martin'。

您还可以在控制器中使用依赖注入,操作响应对象,并在访客的下一次请求中将信息存储在 TransactionCache 中。

关于

要求

Merophp 框架与 PHP 7.4、8.0 和 8.1 兼容。无需其他 PHP 扩展。

支持

请注意,这是一个类似娱乐性质的项目,不支持或进一步维护的保证。也存在很高的波动性风险。