From 97de5ec026074ff9721fe8743396095f34712da0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 26 Mar 2018 17:24:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=93=8D=E4=BD=9C=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 5 +---- library/think/App.php | 7 +++++++ library/think/Controller.php | 5 +---- library/think/Request.php | 5 +++-- 4 files changed, 12 insertions(+), 10 deletions(-) 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); } }