+4
-1
@@ -17,6 +17,9 @@
|
|||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Kiri\\Router\\": "./src"
|
"Kiri\\Router\\": "./src"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"./src/function.php"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,53 +4,10 @@ declare(strict_types=1);
|
|||||||
namespace Kiri\Router;
|
namespace Kiri\Router;
|
||||||
|
|
||||||
use Kiri\Di\Interface\ResponseEmitterInterface;
|
use Kiri\Di\Interface\ResponseEmitterInterface;
|
||||||
use Kiri\Router\Blade\BladeFactory;
|
|
||||||
use Kiri\Router\Blade\BladeHelper;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\StreamInterface;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user