From ac0cb9be421d403c05ec169eaebfa837415a4a40 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 1 Jun 2016 18:34:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=20MODULE=5FNAME=20CONTROLLER?= =?UTF-8?q?=5FNAME=20ACTION=5FNAME=20=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 7 ++- library/think/App.php | 49 ++++++++++--------- library/think/Console.php | 22 ++++----- library/think/Controller.php | 4 +- library/think/Loader.php | 9 ++-- library/think/Request.php | 48 ++++++++++++++++++ library/think/Url.php | 10 ++-- library/think/controller/Rest.php | 2 +- library/think/view/driver/Php.php | 9 ++-- library/think/view/driver/Think.php | 9 ++-- .../thinkphp/library/think/controllerTest.php | 13 ++--- 11 files changed, 123 insertions(+), 59 deletions(-) diff --git a/base.php b/base.php index f418428d..e7766050 100644 --- a/base.php +++ b/base.php @@ -19,17 +19,16 @@ define('LIB_PATH', THINK_PATH . 'library' . DS); define('MODE_PATH', THINK_PATH . 'mode' . DS); // 系统应用模式目录 define('CORE_PATH', LIB_PATH . 'think' . DS); define('TRAIT_PATH', LIB_PATH . 'traits' . DS); - defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS); defined('ROOT_PATH') or define('ROOT_PATH', dirname(APP_PATH) . DS); defined('EXTEND_PATH') or define('EXTEND_PATH', ROOT_PATH . 'extend' . DS); -defined('APP_NAMESPACE') or define('APP_NAMESPACE', 'app'); -defined('COMMON_MODULE') or define('COMMON_MODULE', 'common'); +defined('VENDOR_PATH') or define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DS); defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH . 'log' . DS); defined('CACHE_PATH') or define('CACHE_PATH', RUNTIME_PATH . 'cache' . DS); defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS); -defined('VENDOR_PATH') or define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); +defined('APP_NAMESPACE') or define('APP_NAMESPACE', 'app'); +defined('COMMON_MODULE') or define('COMMON_MODULE', 'common'); defined('CONF_PATH') or define('CONF_PATH', APP_PATH); // 配置文件目录 defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀 defined('APP_MULTI_MODULE') or define('APP_MULTI_MODULE', true); // 是否多模块 diff --git a/library/think/App.php b/library/think/App.php index 5e8dc4b7..7e8f8d14 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -228,63 +228,68 @@ class App } if (APP_MULTI_MODULE) { // 多模块部署 - $module = strtolower($result[0] ?: $config['default_module']); - // 获取模块名称 - define('MODULE_NAME', strip_tags($module)); + $module = strip_tags(strtolower($result[0] ?: $config['default_module'])); $bind = Route::bind('module'); $available = false; if ($bind) { // 绑定模块 list($bindModule) = explode('/', $bind); - if (MODULE_NAME == $bindModule) { + if ($module == $bindModule) { $available = true; } - } elseif (!in_array(MODULE_NAME, $config['deny_module_list']) && is_dir(APP_PATH . MODULE_NAME)) { + } elseif (!in_array($module, $config['deny_module_list']) && is_dir(APP_PATH . $module)) { $available = true; } // 模块初始化 - if (MODULE_NAME && $available) { - define('MODULE_PATH', APP_PATH . MODULE_NAME . DS); + if ($module && $available) { + define('MODULE_PATH', APP_PATH . $module . DS); define('VIEW_PATH', MODULE_PATH . 'view' . DS); // 初始化模块 - $config = self::initModule(MODULE_NAME, $config); + $config = self::initModule($module, $config); } else { - throw new HttpException(404, 'module [ ' . MODULE_NAME . ' ] not exists '); + throw new HttpException(404, 'module [ ' . $module . ' ] not exists '); } } else { // 单一模块部署 - define('MODULE_NAME', ''); + $module = ''; define('MODULE_PATH', APP_PATH); define('VIEW_PATH', MODULE_PATH . 'view' . DS); } // 获取控制器名 - $controllerName = strip_tags($result[1] ?: $config['default_controller']); - defined('CONTROLLER_NAME') or define('CONTROLLER_NAME', $config['url_controller_convert'] ? strtolower($controllerName) : $controllerName); + $controller = strip_tags($result[1] ?: $config['default_controller']); + $controller = $config['url_controller_convert'] ? strtolower($controller) : $controller; // 获取操作名 $actionName = strip_tags($result[2] ?: $config['default_action']); - defined('ACTION_NAME') or define('ACTION_NAME', $config['url_action_convert'] ? strtolower($actionName) : $actionName); + $actionName = $config['url_action_convert'] ? strtolower($actionName) : $actionName; // 执行操作 - if (!preg_match('/^[A-Za-z](\/|\.|\w)*$/', CONTROLLER_NAME)) { + if (!preg_match('/^[A-Za-z](\/|\.|\w)*$/', $controller)) { // 安全检测 - throw new Exception('illegal controller name:' . CONTROLLER_NAME, 10000); + throw new Exception('illegal controller name:' . $controller, 10000); } - $instance = Loader::controller(CONTROLLER_NAME, $config['url_controller_layer'], $config['use_controller_suffix'], $config['empty_controller']); - // 获取当前操作名 - $action = ACTION_NAME . $config['action_suffix']; + + // 设置当前请求的模块、控制器、操作 + $request = Request::instance(); + $request->module($module); + $request->controller($controller); + $request->action($actionName); try { - // 操作方法开始监听 - $call = [$instance, $action]; - Hook::listen('action_begin', $call); + // 获取当前操作名 + $action = $actionName . $config['action_suffix']; if (!preg_match('/^[A-Za-z](\w)*$/', $action)) { // 非法操作 - throw new \ReflectionException('illegal action name :' . ACTION_NAME); + throw new \ReflectionException('illegal action name :' . $actionName); } + $instance = Loader::controller($controller, $config['url_controller_layer'], $config['use_controller_suffix'], $config['empty_controller']); + // 执行操作方法 + $call = [$instance, $action]; + Hook::listen('action_begin', $call); + $data = self::invokeMethod($call); } catch (\ReflectionException $e) { // 操作不存在 diff --git a/library/think/Console.php b/library/think/Console.php index 6e5ad424..c4d96f59 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -38,7 +38,7 @@ class Console private $runningCommand; private $catchExceptions = true; - private $autoExit = true; + private $autoExit = true; private $definition; private $helperSet; private $terminalDimensions; @@ -49,7 +49,7 @@ class Console "think\\console\\command\\Lists", "think\\console\\command\\Build", "think\\console\\command\\make\\Controller", - "think\\console\\command\\make\\Model" + "think\\console\\command\\make\\Model", ]; public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') @@ -90,7 +90,7 @@ class Console $exitCode = $e->getCode(); if (is_numeric($exitCode)) { - $exitCode = (int)$exitCode; + $exitCode = (int) $exitCode; if (0 === $exitCode) { $exitCode = 1; } @@ -201,7 +201,7 @@ class Console */ public function setCatchExceptions($boolean) { - $this->catchExceptions = (bool)$boolean; + $this->catchExceptions = (bool) $boolean; } /** @@ -211,7 +211,7 @@ class Console */ public function setAutoExit($boolean) { - $this->autoExit = (bool)$boolean; + $this->autoExit = (bool) $boolean; } /** @@ -379,7 +379,7 @@ class Console $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]) . '[^:]*'; }, $namespace); - $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); + $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); if (empty($namespaces)) { $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); @@ -417,7 +417,7 @@ class Console $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]) . '[^:]*'; }, $name); - $commands = preg_grep('{^' . $expr . '}', $allCommands); + $commands = preg_grep('{^' . $expr . '}', $allCommands); if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) { if (false !== $pos = strrpos($name, ':')) { @@ -606,19 +606,19 @@ class Console if ('\\' === DS) { if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { - return [(int)$matches[1], (int)$matches[2]]; + return [(int) $matches[1], (int) $matches[2]]; } if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { - return [(int)$matches[1], (int)$matches[2]]; + return [(int) $matches[1], (int) $matches[2]]; } } if ($sttyString = $this->getSttyColumns()) { if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { - return [(int)$matches[2], (int)$matches[1]]; + return [(int) $matches[2], (int) $matches[1]]; } if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { - return [(int)$matches[2], (int)$matches[1]]; + return [(int) $matches[2], (int) $matches[1]]; } } diff --git a/library/think/Controller.php b/library/think/Controller.php index c7086c64..5920d7c0 100644 --- a/library/think/Controller.php +++ b/library/think/Controller.php @@ -69,14 +69,14 @@ class Controller if (is_string($options['only'])) { $options['only'] = explode(',', $options['only']); } - if (!in_array(ACTION_NAME, $options['only'])) { + if (!in_array($this->request->action(), $options['only'])) { return; } } elseif (isset($options['except'])) { if (is_string($options['except'])) { $options['except'] = explode(',', $options['except']); } - if (in_array(ACTION_NAME, $options['except'])) { + if (in_array($this->request->action(), $options['except'])) { return; } } diff --git a/library/think/Loader.php b/library/think/Loader.php index c41f8eee..8353dfae 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -12,6 +12,7 @@ namespace think; use think\exception\HttpException; +use think\Request; class Loader { @@ -276,7 +277,7 @@ class Loader if (strpos($name, '/')) { list($module, $name) = explode('/', $name, 2); } else { - $module = APP_MULTI_MODULE ? MODULE_NAME : ''; + $module = APP_MULTI_MODULE ? Request::instance()->module() : ''; } $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { @@ -311,7 +312,7 @@ class Loader if (strpos($name, '/')) { list($module, $name) = explode('/', $name); } else { - $module = APP_MULTI_MODULE ? MODULE_NAME : ''; + $module = APP_MULTI_MODULE ? Request::instance()->module() : ''; } $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { @@ -346,7 +347,7 @@ class Loader if (strpos($name, '/')) { list($module, $name) = explode('/', $name); } else { - $module = APP_MULTI_MODULE ? MODULE_NAME : ''; + $module = APP_MULTI_MODULE ? Request::instance()->module() : ''; } $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { @@ -385,7 +386,7 @@ class Loader { $info = pathinfo($url); $action = $info['basename']; - $module = '.' != $info['dirname'] ? $info['dirname'] : CONTROLLER_NAME; + $module = '.' != $info['dirname'] ? $info['dirname'] : Request::instance()->controller(); $class = self::controller($module, $layer, $appendSuffix); if ($class) { if (is_scalar($vars)) { diff --git a/library/think/Request.php b/library/think/Request.php index f5810ac0..601edf47 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -67,6 +67,10 @@ class Request */ protected $dispatch = []; + protected $module; + protected $controller; + protected $action; + /** * @var array 请求参数 */ @@ -908,4 +912,48 @@ class Request return $this->dispatch; } + /** + * 设置或者获取当前的模块名 + * @access public + * @param string $module 模块名 + * @return string + */ + public function module($module = null) + { + if (!is_null($module)) { + $this->module = $module; + } else { + return $this->module ?: ''; + } + } + + /** + * 设置或者获取当前的控制器名 + * @access public + * @param string $controller 控制器名 + * @return string + */ + public function controller($controller = null) + { + if (!is_null($controller)) { + $this->controller = $controller; + } else { + return $this->controller ?: ''; + } + } + + /** + * 设置或者获取当前的操作名 + * @access public + * @param string $action 操作名 + * @return string + */ + public function action($action = null) + { + if (!is_null($action)) { + $this->action = $action; + } else { + return $this->action ?: ''; + } + } } diff --git a/library/think/Url.php b/library/think/Url.php index de222ea5..998a1f64 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -13,6 +13,7 @@ namespace think; use think\Cache; use think\Config; +use think\Request; use think\Route; class Url @@ -118,6 +119,7 @@ class Url // 直接解析URL地址 protected static function parseUrl($url) { + $request = Request::instance(); if (0 === strpos($url, '/')) { // 直接作为路由地址解析 $url = substr($url, 1); @@ -129,14 +131,16 @@ class Url $url = substr($url, 1); } else { // 解析到 模块/控制器/操作 - $module = MODULE_NAME ? MODULE_NAME . '/' : ''; + $module = $request->module(); + $module = $module ? $module . '/' : ''; + $controller = $request->controller(); if ('' == $url) { // 空字符串输出当前的 模块/控制器/操作 - $url = $module . CONTROLLER_NAME . '/' . ACTION_NAME; + $url = $module . $controller . '/' . $request->action(); } else { $path = explode('/', $url); $action = array_pop($path); - $controller = empty($path) ? CONTROLLER_NAME : (Config::get('url_controller_convert') ? Loader::parseName(array_pop($path)) : array_pop($path)); + $controller = empty($path) ? $controller : (Config::get('url_controller_convert') ? Loader::parseName(array_pop($path)) : array_pop($path)); $module = empty($path) ? $module : array_pop($path) . '/'; $url = $module . $controller . '/' . $action; } diff --git a/library/think/controller/Rest.php b/library/think/controller/Rest.php index ebf53be8..c31e9287 100644 --- a/library/think/controller/Rest.php +++ b/library/think/controller/Rest.php @@ -76,7 +76,7 @@ abstract class Rest return $this->$fun(); } else { // 抛出异常 - throw new \Exception('error action :' . ACTION_NAME); + throw new \Exception('error action :' . $method); } } diff --git a/library/think/view/driver/Php.php b/library/think/view/driver/Php.php index 9019d7cc..ac101ea5 100644 --- a/library/think/view/driver/Php.php +++ b/library/think/view/driver/Php.php @@ -13,6 +13,7 @@ namespace think\view\driver; use think\Exception; use think\Log; +use think\Request; class Php { @@ -102,14 +103,16 @@ class Php } // 分析模板文件规则 - if (defined('CONTROLLER_NAME') && 0 !== strpos($template, '/')) { + $request = Request::instance(); + $controller = $request->controller(); + if ($controller && 0 !== strpos($template, '/')) { $depr = $this->config['view_depr']; $template = str_replace(['/', ':'], $depr, $template); if ('' == $template) { // 如果模板文件名为空 按照默认规则定位 - $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . ACTION_NAME; + $template = str_replace('.', DS, $controller) . $depr . $request->action(); } elseif (false === strpos($template, $depr)) { - $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template; + $template = str_replace('.', DS, $controller) . $depr . $template; } } return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); diff --git a/library/think/view/driver/Think.php b/library/think/view/driver/Think.php index c70d8d52..ddc3e7b7 100644 --- a/library/think/view/driver/Think.php +++ b/library/think/view/driver/Think.php @@ -13,6 +13,7 @@ namespace think\view\driver; use think\Exception; use think\Log; +use think\Request; use think\Template; class Think @@ -110,14 +111,16 @@ class Think } // 分析模板文件规则 - if (defined('CONTROLLER_NAME') && 0 !== strpos($template, '/')) { + $request = Request::instance(); + $controller = $request->controller(); + if ($controller && 0 !== strpos($template, '/')) { $depr = $this->config['view_depr']; $template = str_replace(['/', ':'], $depr, $template); if ('' == $template) { // 如果模板文件名为空 按照默认规则定位 - $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . ACTION_NAME; + $template = str_replace('.', DS, $controller) . $depr . $request->action(); } elseif (false === strpos($template, $depr)) { - $template = str_replace('.', DS, CONTROLLER_NAME) . $depr . $template; + $template = str_replace('.', DS, $controller) . $depr . $template; } } return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); diff --git a/tests/thinkphp/library/think/controllerTest.php b/tests/thinkphp/library/think/controllerTest.php index 5a4a8397..dbf60d8e 100644 --- a/tests/thinkphp/library/think/controllerTest.php +++ b/tests/thinkphp/library/think/controllerTest.php @@ -18,6 +18,7 @@ namespace tests\thinkphp\library\think; use ReflectionClass; use think\Controller; +use think\Request; use think\View; require_once CORE_PATH . '../../helper.php'; @@ -93,16 +94,16 @@ class controllerTest extends \PHPUnit_Framework_TestCase { public function testInitialize() { - $foo = new Foo; + $foo = new Foo(Request::instance()); $this->assertEquals('abcd', $foo->test); } public function testBeforeAction() { - $obj = new Bar; + $obj = new Bar(Request::instance()); $this->assertEquals(7, $obj->test); - $obj = new Baz; + $obj = new Baz(Request::instance()); $this->assertEquals(19, $obj->test); } @@ -118,7 +119,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase public function testFetch() { - $controller = new Foo; + $controller = new Foo(Request::instance()); $view = $this->getView($controller); $template = dirname(__FILE__) . '/display.html'; $viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']); @@ -138,7 +139,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase public function testAssign() { - $controller = new Foo; + $controller = new Foo(Request::instance()); $view = $this->getView($controller); $controller->assign('abcd', 'dcba'); $controller->assign(['key1' => 'value1', 'key2' => 'value2']); @@ -148,7 +149,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase public function testValidate() { - $controller = new Foo; + $controller = new Foo(Request::instance()); $data = [ 'username' => 'username', 'nickname' => 'nickname',