biurad/slim

此包已被废弃,不再维护。未建议替代包。

一个快速、可编译和可扩展的PHP框架

v1.1.3 2019-03-30 05:54 UTC

This package is auto-updated.

Last update: 2022-02-02 07:37:31 UTC


README

PHP Version Latest Version Workflow Status Code Maintainability Coverage Status Quality Score

divineniiquaye/php-rade 是一个基于 PHP 7.4+ 的极快、微型、可编译和可扩展的框架,基于 PSR-7PSR-11PSR-14PSR-15,支持注解/属性,由 Divine Niiquaye 创建。

这个库旨在帮助那些懒惰的开发者、初学者、小企业或希望用极少的依赖快速构建事物的个人。

PHP Rade Framework Image

📦 安装 & 基本用法

此项目需要 PHP 7.4 或更高版本。推荐的安装方法是使用 Composer。只需运行

$ composer require divineniiquaye/php-rade 2.0.*

Rade 是基于 Flight RoutingRade DISymfony 组件Biurad 库 构建的。Rade 是一个完全符合 PSR 的 PHP 框架,可以完全自定义,甚至可以用于从小型到大型项目的开发。

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

// Boot the application.
$app = new Rade\Application();

// Add a route to application
$app->match('/hello/{name:\w+}', fn (string $name): string => 'Hello ' . $app->escape()->escapeHtml($name));

$extensions = [
    [Rade\DI\Extensions\CoreExtension::class, [__DIR__]],
    // You can add more extensions here ...
];

//If you want to use extensions, here is an example:
$app->loadExtensions($extensions, ['config' => ['debug' => $_ENV['APP_DEBUG'] ?? false]]);

// You can set custom pages for caught exceptions, using default event dispatcher, or your custom event dispatcher.
$app->getDispatcher()->addListener(Rade\Events::EXCEPTION, new ErrorListener(), -8);

$app->run();

在处理大型项目时,建议使用应用的缓存版本。这比使用不带扩展的不可缓存的 Application 类提高了超过 100% 的性能。

use function Rade\DI\Loader\{phpCode, wrap};

$config = [
    'cacheDir' => __DIR__ . '/caches',
    'debug' => $_ENV['APP_DEBUG'] ?? false, // Set the debug mode environment
];

// Setup cache for application.
$app = \Rade\AppBuilder::build(static function (\Rade\AppBuilder $creator): void {
    // Add resource to re-compile if changes are made to this file.
    $creator->addResource(new FileResource(__FILE__));

    // Adding routes requires the Rade\DI\Extensions\RoutingExtension to be loaded.
    // Routes should always be added before Rade\DI\Extensions\RoutingExtension is booted, else it will not be compiled.
    $creator->match('/hello/{name:\w+}', to: phpCode('fn (string $name): string => \'Hello \' . $this->escape()->escapeHtml($name);'));

    $extensions = [
        [Rade\DI\Extensions\CoreExtension::class, [__DIR__]],
        // You can add more extensions here ...
    ];

    //If you want to use extensions, here is an example as its recommended to use extensions to build your application.
    $app->loadExtensions($extensions, ['config' => ['debug' => $creator->isDebug()]]);

    // You can set custom pages for caught exceptions, using default event dispatcher, or your custom event dispatcher.
    $creator->definition('events.dispatcher')->bind('addListener', [Rade\Events::EXCEPTION, wrap(ErrorListener::class), -8]);
}, $config);

$app->run(); // Boot the application.

以下是一个您可以为您的应用程序使用的自定义错误示例。

use Biurad\Http\Response\HtmlResponse;
use Rade\Event\ExceptionEvent;

class ErrorListener
{
    public function __invoke(ExceptionEvent $event): void
    {
        // If extensions were loaded, the %project_dir% will exist, else replace will absolute path
        $errorsPath = $event->getApplication()->parameter('%project_dir%/errors/');

        $code = $event->getThrowable()->getCode();
        $templates = [
            $errorsPath . \substr($code, 0, 2) . 'x.html.php', // 40x.html.php format ...
            $errorsPath . \substr($code, 0, 1) . 'xx.html.php', // 4xx.html.php format ...
            $errorsPath . $code . '.html.php', // 404.html.php format ...
            $errorsPath . 'default.html.php',
        ];

        // Tries to load a template file from a list of error templates.
        foreach ($template as $template) {
            if (\file_exists($template)) {
                $event->setResponse(
                    (static function () use ($template, $code) {
                        \ob_start();
                        include __DIR__ . $template;

                        return new HtmlResponse(\ob_get_clean(), (int) $code);
                    })()
                );
            }
        }
    }
}

需要注意的是,使用 PSR-15 中间件堆栈时,使用了 PHP SPL 队列类,其算法如下:LAST <- FIRST : FIRST -> LAST。此外,在加载扩展和通过优先级向默认事件分发器添加事件监听器时,数字越高,扩展或事件监听器在链中的触发越早(默认为 0)。

📓 文档

在使用此库之前,请查看详细的文档。有关高级用法、配置和定制的完整文档,请访问 docs.divinenii.com

⏫ 升级

有关如何升级到此库的新版本的信息,请参阅 UPGRADE

🏷️ 更新日志

SemVer 遵循严格。小版本和补丁版本不应引入破坏性更改到代码库中;更多信息请参阅 CHANGELOG

标记为 @internal 的任何类或方法都不建议在此库外部使用,并且可能随时发生破坏性更改,因此请避免使用它们。

🛠️ 维护与支持

(此政策可能在未来发生变化,并且可能根据具体情况做出例外。)

  • 新的 补丁版本 大约每月发布一次(例如 1.0.101.1.6)。它仅包含错误修复,因此您可以安全地升级您的应用程序。
  • 新的 小版本 每六个月发布一次:一次在六月,一次在十二月。它包含错误修复和新功能,但不包括任何破坏性更改,因此您可以安全地升级您的应用程序;
  • 新的 主要版本 每两年发布一次(例如 1.02.03.0)。它可能包含破坏性更改,因此您可能需要在升级应用程序之前进行一些更改。

当发布一个 主要 版本时,每个分支的小版本数量限制为五个(X.0,X.1,X.2,X.3 和 X.4)。分支的最后一个小版本(例如 1.4,2.4)被视为 长期支持(LTS)版本,持续超过 2 年,其他版本可维持 8 个月。

在发布版本的主动维护结束后,可从 Biurad Lap 获得专业支持.

🧪 测试

$ ./vendor/bin/phpunit

这将测试 divineniiquaye/php-rade 是否能够在 PHP 7.4 或更高版本上运行。

🏛️ 治理

该项目主要由 Divine Niiquaye Ibok 维护。欢迎贡献 👷‍♀️!要贡献,请熟悉我们的 CONTRIBUTING 指南。

要报告安全漏洞,请使用 Biurad Security。我们将协调修复,并将最终将解决方案提交到本项目中。

🙌 赞助商

您有兴趣赞助此项目的开发吗?请与我们联系,并在 Patreon 上支持我们,或查看 https://biurad.com/sponsor 以了解贡献方式。

👥 信用 & 致谢

🗺️ 使用者

您可以使用此软件包,但如果它进入您的生产环境,我们非常感激您通过 电子邮件消息 提及此库。我们将公布所有收到的请求,请访问 https://patreons.biurad.com

查看其他人如何使用 divineniiquaye/php-rade 做一些酷炫的事情:https://packagist.org.cn/packages/divineniiquaye/php-rade/dependents

📄 许可证

divineniiquaye/php-rade 库版权 © Divine Niiquaye Ibok,并许可在以下协议下使用:软件许可证