webman迁移-优化curd

This commit is contained in:
2026-03-18 15:24:32 +08:00
parent e2ae55319e
commit a64d157388
4 changed files with 31 additions and 11 deletions

View File

@@ -424,13 +424,17 @@ class Helper
return root_path() . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'crud' . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . Filesystem::fsFit($name) . '.stub'; return root_path() . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'crud' . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . Filesystem::fsFit($name) . '.stub';
} }
public static function arrayToString(array|string $value): string public static function arrayToString(mixed $value): string
{ {
if (!is_array($value)) return $value; if (is_array($value)) {
foreach ($value as &$item) { foreach ($value as &$item) {
$item = self::arrayToString($item); $item = self::arrayToString($item);
}
return implode(PHP_EOL, $value);
} }
return implode(PHP_EOL, $value); if (is_string($value)) return $value;
if (is_bool($value)) return $value ? 'true' : 'false';
return $value === null ? '' : strval($value);
} }
public static function assembleStub(string $name, array $data, bool $escape = false): string public static function assembleStub(string $name, array $data, bool $escape = false): string

View File

@@ -11,10 +11,10 @@ class {%className%} extends Backend
{ {
/** /**
* {%modelName%}模型对象 * {%modelName%}模型对象
* @var object * @var object|null
* @phpstan-var \{%modelNamespace%}\{%modelName%} * @phpstan-var \{%modelNamespace%}\{%modelName%}|null
*/ */
protected object $model; protected ?object $model = null;
{%attr%}{%initialize%} {%attr%}{%initialize%}
{%methods%} {%methods%}

View File

@@ -377,11 +377,16 @@ if (!function_exists('parse_name')) {
if (!function_exists('root_path')) { if (!function_exists('root_path')) {
/** /**
* 根路径BuildAdmin 兼容,等价于 base_path * 根路径BuildAdmin 兼容,等价于 base_path
* 无参数时返回带尾部分隔符的路径,确保 root_path() . 'app' 拼接正确
* @param string $path 子路径 * @param string $path 子路径
*/ */
function root_path(string $path = ''): string function root_path(string $path = ''): string
{ {
return base_path($path); $base = base_path($path);
if ($path === '' && $base !== '') {
return rtrim($base, DIRECTORY_SEPARATOR . '/') . DIRECTORY_SEPARATOR;
}
return $base;
} }
} }

View File

@@ -249,8 +249,19 @@ Route::add(
if (!method_exists($class, $action)) { if (!method_exists($class, $action)) {
return new Response(404, ['Content-Type' => 'application/json'], json_encode(['code' => 404, 'msg' => '404 Not Found', 'data' => []], JSON_UNESCAPED_UNICODE)); return new Response(404, ['Content-Type' => 'application/json'], json_encode(['code' => 404, 'msg' => '404 Not Found', 'data' => []], JSON_UNESCAPED_UNICODE));
} }
$instance = new $class(); // 设置 controller 供 get_controller_path、权限校验等使用
return $instance->$action($request); $request->controller = $class;
try {
$instance = new $class();
return $instance->$action($request);
} catch (\Throwable $e) {
return new Response(500, ['Content-Type' => 'application/json'], json_encode([
'code' => 0,
'msg' => $e->getMessage(),
'time' => time(),
'data' => null,
], JSON_UNESCAPED_UNICODE));
}
} }
); );