diff --git a/helper.php b/helper.php index b568dab1..c2e7ffe9 100644 --- a/helper.php +++ b/helper.php @@ -444,10 +444,7 @@ if (!function_exists('view')) { function view($template = '', $vars = [], $replace = [], $code = 200) { if ('' === $template) { - $trace = debug_backtrace(false, 2); - $suffix = Config::get('action_suffix'); - $action = $suffix ? substr($trace[1]['function'], 0, -strlen($suffix)) : $trace[1]['function']; - $template = Loader::parseName($action); + $template = Loader::parseName(request()->action(true)); } return Response::create($template, 'view', $code)->replace($replace)->assign($vars); } diff --git a/library/think/App.php b/library/think/App.php index d34c04cb..4ef580fc 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -581,6 +581,13 @@ class App if (is_callable([$instance, $action])) { // 执行操作方法 $call = [$instance, $action]; + // 严格获取当前操作方法名 + $reflect = new \ReflectionMethod($instance, $action); + $methodName = $reflect->getName(); + $suffix = $config['action_suffix']; + $actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName; + $request->action($actionName); + } elseif (is_callable([$instance, '_empty'])) { // 空操作 $call = [$instance, '_empty']; diff --git a/library/think/Controller.php b/library/think/Controller.php index 43044de7..80b77d90 100644 --- a/library/think/Controller.php +++ b/library/think/Controller.php @@ -118,10 +118,7 @@ class Controller protected function fetch($template = '', $vars = [], $replace = [], $config = []) { if ('' === $template) { - $trace = debug_backtrace(false, 2); - $suffix = Config::get('action_suffix'); - $action = $suffix ? substr($trace[1]['function'], 0, -strlen($suffix)) : $trace[1]['function']; - $template = Loader::parseName($action); + $template = Loader::parseName($this->request->action(true)); } return $this->view->fetch($template, $vars, $replace, $config); diff --git a/library/think/Request.php b/library/think/Request.php index 4a40d22c..b51e4722 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -1466,11 +1466,12 @@ class Request */ public function action($action = null) { - if (!is_null($action)) { + if (!is_null($action) && !is_bool($action)) { $this->action = $action; return $this; } else { - return $this->action ?: ''; + $name = $this->action ?: ''; + return true === $action ? $name : strtolower($name); } }