This commit is contained in:
2025-12-01 06:39:04 +08:00
parent 79ce7142d9
commit 89ac36c227
10 changed files with 1093 additions and 14 deletions
+23 -14
View File
@@ -4,13 +4,17 @@ declare(strict_types=1);
namespace Kiri\Router;
use Kiri\Di\Interface\ResponseEmitterInterface;
use Kiri\Router\Blade\BladeFactory;
use Kiri\Router\Blade\BladeHelper;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* @param string $path
* @param array $data
* 渲染 Blade 视图
*
* @param string $path 视图路径(支持 . 分隔,如 'user.profile'
* @param array $data 视图数据
*
* @return ResponseInterface
*/
@@ -18,21 +22,26 @@ function View(string $path, array $data = []): ResponseInterface
{
$response = \response();
$response->withAddedHeader('Content-Type', 'text/html; charset=utf-8');
try {
ob_start();
$__path = $path;
$__data = $data;
extract($__data, EXTR_SKIP);
require APP_PATH . 'resources/view/' . $path . '.php';
$content = ltrim(ob_get_clean());
// 获取视图路径和缓存路径
$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);
}
// 渲染视图
$content = $factory->render($path, $data);
} catch (\Exception $e) {
$content = throwable($e);
} finally {
return $response->html($content);
$content = function_exists('throwable') ? throwable($e) : $e->getMessage();
}
return $response->html($content);
}
/**