slim/php-view
Composer 安装命令:
composer require slim/php-view
包简介
Render PHP view scripts into a PSR-7 Response object.
README 文档
README
PHP Renderer
This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 4.
Cross-site scripting (XSS) risks
Note that PHP-View has no built-in mitigation from XSS attacks.
It is the developer's responsibility to use htmlspecialchars()
or a component like laminas-escaper. Alternatively, consider Twig-View.
Installation
composer require slim/php-view
Usage with any PSR-7 Project
//Construct the View $renderer = new PhpRenderer('path/to/templates'); $viewData = [ 'key1' => 'value1', 'key2' => 'value2', ]; // Render a template $response = $renderer->render(new Response(), 'hello.php', $viewData);
Usage with Slim 4
use Slim\AppFactory; use Slim\Views\PhpRenderer; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/hello', function ($request, $response) { $renderer = new PhpRenderer('path/to/templates'); $viewData = [ 'name' => 'John', ]; return $renderer->render($response, 'hello.php', $viewData); }); $app->run();
DI Container Setup
You can place the PhpRenderer instantiation within your DI Container.
<?php use Psr\Container\ContainerInterface; use Slim\Views\PhpRenderer; // ... return [ PhpRenderer::class => function (ContainerInterface $container) { $renderer = new PhpRenderer('path/to/templates'); return $renderer; }, ];
Template Variables
You can now add variables to your renderer that will be available to all templates you render.
// Via the constructor $globalViewData = [ 'title' => 'Title' ]; $renderer = new PhpRenderer('path/to/templates', $globalViewData); // or setter $viewData = [ 'key1' => 'value1', 'key2' => 'value2', ]; $renderer->setAttributes($viewData); // or individually $renderer->addAttribute($key, $value);
Data passed in via the render() method takes precedence over attributes.
$viewData = [ 'title' => 'Title' ]; $renderer = new PhpRenderer('path/to/templates', $viewData); //... $response = $renderer->render($response, $template, [ 'title' => 'My Title' ]); // In the view above, the $title will be "My Title" and not "Title"
Sub-templates
Inside your templates you may use $this to refer to the PhpRenderer object to render sub-templates.
If using a layout the fetch() method can be used instead of render() to avoid applying the layout to the sub-template.
<?=$this->fetch('./path/to/partial.phtml', ['name' => 'John'])?>
Rendering in Layouts
You can now render view in another views called layouts, this allows you to compose modular view templates and help keep your views DRY.
Create your layout path/to/templates/layout.php
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
Create your view template path/to/templates/hello.php
Hello <?=$name?>!
Rendering in your code.
$renderer = new PhpRenderer('path/to/templates', ['title' => 'My App']); $renderer->setLayout('layout.php'); $viewData = [ 'title' => 'Hello - My App', 'name' => 'John', ]; //... $response = $renderer->render($response, 'hello.php', $viewData);
Response will be
<html><head><title>Hello - My App</title></head><body>Hello John!</body></html>
Please note, the $content is special variable used inside layouts
to render the wrapped view and should not be set in your view parameters.
Escaping values
It's essential to ensure that the HTML output is secure to prevent common web vulnerabilities like Cross-Site Scripting (XSS). This package has no built-in mitigation from XSS attacks.
The following function uses the htmlspecialchars function
with specific flags to ensure proper encoding:
function html(?string $text = null): string { return htmlspecialchars($text ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
You could consider setting it up as a global function in composer.json.
Usage
Hello <?= html($name) ?>
Exceptions
\Slim\Views\Exception\PhpTemplateNotFoundException- If template layout does not exist\Slim\Views\Exception\PhpTemplateNotFoundException- If template does not exist\RuntimeException- If the template output could not be fetched\InvalidArgumentException- If $data contains 'template'
slim/php-view 适用场景与选型建议
slim/php-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.95M 次下载、GitHub Stars 达 274, 最近一次更新时间为 2015 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「template」 「view」 「renderer」 「slim」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 slim/php-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slim/php-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 slim/php-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Simple ASCII output of array data
E2E Studios PHP Framework adaptation of leafsphp/blade package
Alfabank REST API integration
Ensolab template project
Account Balance / Wallets Manager For FilamentPHP and Filament Account Builder
统计信息
- 总下载量: 9.95M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 284
- 点击次数: 29
- 依赖项目数: 109
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-29