hawkbit / presentation
Hawkbit PSR-7 Micro PHP 框架的展示层
Requires
- php: >=5.6
- league/plates: ~3.0
- psr/container: ~1.0
- psr/http-message: ~1.0
Requires (Dev)
- hawkbit/hawkbit: ~2.3
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-09-13 21:57:00 UTC
README
多语言且可扩展的展示层,适用于不同的展示引擎。展示层使用默认的 league/plates
引擎,并可扩展为 Twig、Smarty、Liquid、Blade 等。
安装
使用 Composer
Hawkbit 展示层可在 Packagist 上找到,并可以使用 Composer 安装。这可以通过运行以下命令或更新您的 composer.json
文件来实现。
composer require hawkbit/Presentation
composer.json
{ "require": { "hawkbit/Presentation": "~1.0" } }
请确保也将您的 Composer 自动加载文件包含到您的项目中
<?php require __DIR__ . '/vendor/autoload.php';
下载 .zip 文件
该项目还可在 GitHub 上作为 .zip
文件下载。访问 发布页面,选择您想要的版本,然后点击“源代码(zip)”下载按钮。
要求
此版本支持以下版本的 PHP。
- PHP 5.5
- PHP 5.6
- PHP 7.0
- PHP 7.1
- HHVM
除了 PHP 之外,您还需要一个有效的 PSR-7 和 PSR-11 集成。
Hawkbit Micro 框架 默认支持。
Silex、Lumen、zend-expressive 和 Slim 的支持未经测试,但应该也可以正常工作。
设置
使用现有应用程序配置进行设置(我们参考 tests/assets/config.php)
<?php use \Hawkbit\Application; use \Hawkbit\Presentation\PresentationService; use \Hawkbit\Presentation\Adapters\PlatesAdapter; use \Hawkbit\Presentation\Adapters\Adapter; $app = new Application(require './config.php'); // or configure manually $app = new Application(); $app[Adapter::class] = new PlatesAdapter([ 'default' => __DIR__ . '/path/to/templates', 'another' => __DIR__ . '/path/to/other/templates', ]); $app[PresentationService::class] = new PresentationService($app->getContainer());
Hawkbit 应用程序的展示
<?php /** @var \Hawkbit\Presentation\PresentationService $Presentation */ $service = $app[\Hawkbit\Presentation\PresentationService::class];
Hawkbit 控制器中的展示
在控制器中访问展示服务。Hawkbit 默认将类注入到控制器中。
<?php use Hawkbit\Presentation\PresentationService; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class MyController { /** * @var PresentationService */ private $presentationService; /** * TestInjectableController constructor. * @param PresentationService $presentationService */ public function __construct(PresentationService $presentationService) { $this->presentationService = $presentationService; } public function getIndex(ServerRequestInterface $request, ResponseInterface $response, array $args = []) { $response->getBody()->write($this->presentationService->render('index', ['world' => 'World'])); return $response; } }
访问和扩展引擎
在某些情况下,您可能想要扩展或访问 plates。我们建议在应用程序的中央点扩展 plates,例如在 bootstrap 中,或者在项目服务提供者中更好。
<?php use Hawkbit\Presentation\PresentationService; /** @var PresentationService $service */ $service = $app->getContainer()->get(PresentationService::class); $service->getEngine() ->addFolder('acme', __DIR__ . '/templates/acme') ->registerFunction('uppercase', function ($string) { return strtoupper($string); });
包装成 PSR 7
Hawkbit 展示层提供了一个 PSR-7 包装器,用于将渲染输出捕获到 psr 7 响应中。
请注意,您需要添加一个额外的 PSR-7 实现!
您只需将您最喜欢的展示适配器包装成 psr 7 适配器即可
<?php use \Hawkbit\Application; use \Hawkbit\Presentation\PresentationService; use \Hawkbit\Presentation\Adapters\PlatesAdapter; use \Hawkbit\Presentation\Adapters\Adapter; use \Hawkbit\Presentation\Adapters\Psr7WrapperAdapter; $app = new Application(require './config.php'); // or configure manually $app = new Application(); $app[Adapter::class] = new Psr7WrapperAdapter(new PlatesAdapter([ 'default' => __DIR__ . '/path/to/templates', 'another' => __DIR__ . '/path/to/other/templates', ]), $app[\Psr\Http\Message\ServerRequestInterface::class], $app[\Psr\Http\Message\ResponseInterface::class]); $app[PresentationService::class] = new PresentationService($app->getContainer());
该集成与上述示例一起工作
渲染
请注意,现在 render 方法返回的是 \Psr\Http\Message\ResponseInterface
实例,而不是字符串!
现在,您的展示逻辑(例如在控制器中)如下所示
<?php use Hawkbit\Presentation\PresentationService; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class MyController { /** * @var PresentationService */ private $presentationService; /** * TestInjectableController constructor. * @param PresentationService $presentationService */ public function __construct(PresentationService $presentationService) { $this->presentationService = $presentationService; } public function getIndex(ServerRequestInterface $request, ResponseInterface $response, array $args = []) { // configured with PSR-7 adapter return $this->presentationService->render('index', ['world' => 'World']); } }
在渲染时添加响应
响应类默认附加在渲染过程中。但在某些情况下,您可能需要在渲染之前添加自己的响应类。包装器的 render 方法将可选的响应作为第三个参数。
<?php use Hawkbit\Presentation\PresentationService; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; class MyController { /** * @var PresentationService */ private $presentationService; /** * TestInjectableController constructor. * @param PresentationService $presentationService */ public function __construct(PresentationService $presentationService) { $this->presentationService = $presentationService; } public function getIndex(ServerRequestInterface $request, ResponseInterface $response, array $args = []) { // manipulate response // for example we need to add an api key $response = $response->withHeader('API-KEY', 123); // configured with PSR-7 adapter return $this->presentationService->render('index', ['world' => 'World'], $response); } }
现在您可以使用 渲染不同的视图,例如 $presentationService->render('acme::index')
,并在视图(模板)中使用 视图辅助函数。
Plates
有关更多信息,请参阅 Plates 文档。
变更日志
有关最近更改的详细信息,请参阅 变更日志。
测试
$ composer test
贡献
有关详细信息,请参阅 贡献指南。
安全
如果您发现任何安全相关的问题,请通过电子邮件 mjls@web.de 联系我们,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。