From a64d157388893bf62a1c05bc6ef78b7dfa8830fb Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Wed, 18 Mar 2026 15:24:32 +0800 Subject: [PATCH] =?UTF-8?q?webman=E8=BF=81=E7=A7=BB-=E4=BC=98=E5=8C=96curd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dafuweng-webman/app/admin/library/crud/Helper.php | 14 +++++++++----- .../crud/stubs/mixins/controller/controller.stub | 6 +++--- dafuweng-webman/app/functions.php | 7 ++++++- dafuweng-webman/config/route.php | 15 +++++++++++++-- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/dafuweng-webman/app/admin/library/crud/Helper.php b/dafuweng-webman/app/admin/library/crud/Helper.php index 2ddcf33..c5f1761 100644 --- a/dafuweng-webman/app/admin/library/crud/Helper.php +++ b/dafuweng-webman/app/admin/library/crud/Helper.php @@ -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'; } - public static function arrayToString(array|string $value): string + public static function arrayToString(mixed $value): string { - if (!is_array($value)) return $value; - foreach ($value as &$item) { - $item = self::arrayToString($item); + if (is_array($value)) { + foreach ($value as &$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 diff --git a/dafuweng-webman/app/admin/library/crud/stubs/mixins/controller/controller.stub b/dafuweng-webman/app/admin/library/crud/stubs/mixins/controller/controller.stub index 7a5213f..942eab6 100644 --- a/dafuweng-webman/app/admin/library/crud/stubs/mixins/controller/controller.stub +++ b/dafuweng-webman/app/admin/library/crud/stubs/mixins/controller/controller.stub @@ -11,10 +11,10 @@ class {%className%} extends Backend { /** * {%modelName%}模型对象 - * @var object - * @phpstan-var \{%modelNamespace%}\{%modelName%} + * @var object|null + * @phpstan-var \{%modelNamespace%}\{%modelName%}|null */ - protected object $model; + protected ?object $model = null; {%attr%}{%initialize%} {%methods%} diff --git a/dafuweng-webman/app/functions.php b/dafuweng-webman/app/functions.php index 7a7fb1a..89a08a7 100644 --- a/dafuweng-webman/app/functions.php +++ b/dafuweng-webman/app/functions.php @@ -377,11 +377,16 @@ if (!function_exists('parse_name')) { if (!function_exists('root_path')) { /** * 根路径(BuildAdmin 兼容,等价于 base_path) + * 无参数时返回带尾部分隔符的路径,确保 root_path() . 'app' 拼接正确 * @param string $path 子路径 */ 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; } } diff --git a/dafuweng-webman/config/route.php b/dafuweng-webman/config/route.php index 93cb59b..ea1a5f3 100644 --- a/dafuweng-webman/config/route.php +++ b/dafuweng-webman/config/route.php @@ -249,8 +249,19 @@ Route::add( 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)); } - $instance = new $class(); - return $instance->$action($request); + // 设置 controller 供 get_controller_path、权限校验等使用 + $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)); + } } );