stratify/framework

Stratify 框架包

0.8.0 2017-08-05 20:14 UTC

This package is auto-updated.

Last update: 2024-09-19 20:48:15 UTC


README

Stratify 是一个基于中间件的 Web 框架的尝试。

正在进行中

快速入门

  • PHP 7.1 及以上版本
  • 面向中间件的 HTTP 栈
  • http-interop 中间件
  • 使用 PHP-DI 进行依赖注入和配置
  • 基于 PHP-DI 内核 的模块系统
  • 带有 Silly 的 CLI 控制台

文档可能不是最新的,项目很可能会有变化,待办事项

  • 移除魔法和架构
  • 实现 PSR-15

入门指南

composer require stratify/framework

web/index.php 的示例

<?php

use Stratify\ErrorHandlerModule\ErrorHandlerMiddleware;
use Stratify\Framework\Application;
use Zend\Diactoros\Response\HtmlResponse;
use function Stratify\Framework\pipe;
use function Stratify\Framework\router;

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

// Prepare the HTTP stack
$http = pipe([
    // This is a list of middlewares
    
    // The first middleware is the error handler, it will
    // catch all errors and display the error page
    ErrorHandlerMiddleware::class,
    
    // The application's router
    // See https://github.com/stratifyphp/router for more details
    router([
        // Routes
        '/' => function () {
            return new HtmlResponse('Welcome!');
        },
        '/about' => function () {
            return new HtmlResponse('More information about us');
        },
    ]),
]);

// List of packages containing PHP-DI config to include
// See https://github.com/PHP-DI/Kernel for more details
$modules = [
    'stratify/error-handler-module', // error handling
];

// The application environment
$env = 'dev';

$app = new Application($modules, $env, $http);

// Run the HTTP application
$app->http()->run();

添加 PHP-DI 配置

PHP-DI 配置由 PHP-DI 内核 管理,阅读其文档 了解更多信息。以下是一个简短的示例。

composer.json 中设置您的应用程序名称,这将作为您的 模块 名称被 PHP-DI 内核使用

{
    "name": "app",
    ...
}

您可以使用任何名称,app 是一个好默认值(类似于 Symfony 的 AppBundle)。

将您的模块名称添加到要加载的 PHP-DI 模块列表中

$modules = [
    'stratify/error-handler-module',
    'app',
];

Twig

安装 Twig 模块

composer require stratify/twig-module

在 PHP-DI 模块列表中启用包模块

$modules = [
    'stratify/error-handler-module',
    'stratify/twig-module',
    ...
];

您可以使用 PHP-DI 配置来配置存储视图的目录(请参阅上面的部分)

return [
    ...

    'twig.paths' => [
        // Configure the directory using the alias `app`
        // In this example views are stored in the `res/views/` directory
        // You can then use the `@app/...` notation to render or include Twig templates
        'app' => __DIR__ . '/../views',
    ],
];

然后您可以将 Twig_Environment 类注入到您的服务中,或者在您的控制器中。例如

$http = pipe([
    ErrorHandlerMiddleware::class,
    router([
        // Routes
        '/' => function (Twig_Environment $twig) {
            return $twig->render('@app/home.twig');
        },
    ]),
]);