slim / php-view
将 PHP 视图脚本渲染成 PSR-7 响应对象。
3.4.0
2024-07-19 18:54 UTC
Requires
- php: ^7.4 || ^8.0
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^1
- phpunit/phpunit: ^9 || ^10
- slim/psr7: ^1.6
- squizlabs/php_codesniffer: ^3.10
README
PHP 渲染器
这是一个将 PHP 视图脚本渲染成 PSR-7 响应对象的渲染器。它与 Slim 框架 4 兼容得很好。
跨站脚本(XSS)风险
注意,PHP-View 没有内置的 XSS 攻击缓解措施。使用 htmlspecialchars()
或类似 laminas-escaper 的组件是开发者的责任。或者,可以考虑使用 Twig-View。
安装
composer require slim/php-view
与任何 PSR-7 项目一起使用
//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);
与 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 容器设置
您可以将 PhpRenderer
的实例化放在您的 DI 容器中。
<?php use Psr\Container\ContainerInterface; use Slim\Views\PhpRenderer; // ... return [ PhpRenderer::class => function (ContainerInterface $container) { $renderer = new PhpRenderer('path/to/templates'); return $renderer; }, ];
模板变量
现在您可以添加变量到您的渲染器,这些变量将可用于您渲染的所有模板。
// 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);
通过 render()
方法传入的数据优先于属性。
$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"
子模板
在您的模板中,您可以使用 $this
来引用 PhpRenderer 对象以渲染子模板。如果使用布局,可以使用 fetch()
方法代替 render()
,以避免将布局应用于子模板。
<?=$this->fetch('./path/to/partial.phtml', ['name' => 'John'])?>
在布局中渲染
现在您可以在称为布局的另一个视图中渲染视图,这允许您组合模块化视图模板,并有助于保持视图 DRY。
创建您的布局 path/to/templates/layout.php
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
创建您的视图模板 path/to/templates/hello.php
Hello <?=$name?>!
在您的代码中渲染。
$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);
响应将是
<html><head><title>Hello - My App</title></head><body>Hello John!</body></html>
请注意,$content
是在布局内部用于渲染包装视图的特殊变量,不应在视图参数中设置。
转义值
确保 HTML 输出安全,以防止常见的 Web 漏洞,如跨站脚本(XSS),至关重要。此包没有内置的 XSS 攻击缓解措施。
以下函数使用 htmlspecialchars
函数和特定的标志以确保正确的编码
function html(?string $text = null): string { return htmlspecialchars($text ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
您可以考虑将其设置为一个全局函数在 composer.json。
用法
Hello <?= html($name) ?>
异常
\Slim\Views\Exception\PhpTemplateNotFoundException
- 如果模板布局不存在\Slim\Views\Exception\PhpTemplateNotFoundException
- 如果模板不存在\RuntimeException
- 如果无法获取模板输出\InvalidArgumentException
- 如果 $data 包含 'template'