diff --git a/composer.json b/composer.json index 563aea5..8022fd5 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,9 @@ "autoload": { "psr-4": { "Kiri\\Router\\": "./src" - } + }, + "files": [ + "./src/function.php" + ] } } diff --git a/src/Response.php b/src/Response.php index 0782895..7cc6ebb 100644 --- a/src/Response.php +++ b/src/Response.php @@ -4,53 +4,10 @@ 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; -/** - * 渲染 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); - } -} - /** * */ diff --git a/src/function.php b/src/function.php new file mode 100644 index 0000000..2e9a77f --- /dev/null +++ b/src/function.php @@ -0,0 +1,45 @@ +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); + } +} \ No newline at end of file