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 Framework 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攻击缓解措施。
以下函数使用带有特定标志的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'