From 8531ec89adc8dbaa6ac3fbdb8c0f1cf575923be9 Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Tue, 25 Feb 2025 10:50:18 +0800 Subject: [PATCH] fix(admin): handle exceptions when reflecting on controller methods - Add try-catch block around ReflectionMethod usage - Catch and ignore any Throwable that occurs during reflection - Improve error handling and prevent potential fatal errors --- app/admin/middleware/CheckLogin.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/admin/middleware/CheckLogin.php b/app/admin/middleware/CheckLogin.php index e9a3108..ffa53e4 100644 --- a/app/admin/middleware/CheckLogin.php +++ b/app/admin/middleware/CheckLogin.php @@ -33,13 +33,16 @@ class CheckLogin if (in_array($action, $noNeedCheck)) { return $next($request); } - $reflectionMethod = new \ReflectionMethod($controllerClass, $action); - $attributes = $reflectionMethod->getAttributes(MiddlewareAnnotation::class); - foreach ($attributes as $attribute) { - $annotation = $attribute->newInstance(); - $_ignore = (array)$annotation->ignore; - // 控制器中的某个方法忽略登录 - if (in_array('LOGIN', $_ignore)) return $next($request); + try { + $reflectionMethod = new \ReflectionMethod($controllerClass, $action); + $attributes = $reflectionMethod->getAttributes(MiddlewareAnnotation::class); + foreach ($attributes as $attribute) { + $annotation = $attribute->newInstance(); + $_ignore = (array)$annotation->ignore; + // 控制器中的某个方法忽略登录 + if (in_array('LOGIN', $_ignore)) return $next($request); + } + }catch (\Throwable) { } if (empty($adminUserInfo)) { return redirect(__url('login/index'));