76d2d2c64e
Signed-off-by: 向林 <as2252258@163.com>
48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?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);
|
||
}
|
||
} |