Files
kiri-router/src/function.php
T
as2252258 76d2d2c64e xxx
Signed-off-by: 向林 <as2252258@163.com>
2025-12-31 06:47:28 +00:00

48 lines
1.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use Psr\Http\Message\ResponseInterface;
use Kiri\Router\Blade\BladeFactory;
use Kiri\Router\Blade\BladeHelper;
/**
* 渲染 Blade 视图
*
* @param string $path 视图路径(支持 . 分隔,如 'user.profile'
* @param array $data 视图数据
*
* @return ResponseInterface
*/
function View(string $path, array $data = []): ResponseInterface
{
$response = \response();
$response->withAddedHeader('Content-Type', 'text/html; charset=utf-8');
try {
// 获取视图路径和缓存路径
$viewPath = APP_PATH . 'resources/view';
$cachePath = storage(null, 'view/cache');
// 创建或获取 BladeFactory 实例
$factory = BladeHelper::getFactory();
if ($factory->getViewPath() !== $viewPath) {
$factory = new BladeFactory($viewPath, $cachePath);
BladeHelper::setFactory($factory);
}
// 渲染视图
return $response->html($factory->render($path, $data));
} catch (\Exception $throwable) {
\Kiri::getLogger()->json_log($throwable);
ob_start();
extract(['errorData' => $throwable], EXTR_SKIP);
include __DIR__.'/template/error.php';
$message = ob_get_clean();
return $response->html($message);
}
}