去除 MODULE_NAME CONTROLLER_NAME ACTION_NAME 常量

This commit is contained in:
thinkphp
2016-06-01 18:34:36 +08:00
parent 4a4c53bf6b
commit ac0cb9be42
11 changed files with 123 additions and 59 deletions

View File

@@ -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); // 是否多模块

View File

@@ -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) {
// 操作不存在

View File

@@ -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;
}
/**
@@ -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]];
}
}

View File

@@ -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;
}
}

View File

@@ -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)) {

View File

@@ -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 ?: '';
}
}
}

View File

@@ -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;
}

View File

@@ -76,7 +76,7 @@ abstract class Rest
return $this->$fun();
} else {
// 抛出异常
throw new \Exception('error action :' . ACTION_NAME);
throw new \Exception('error action :' . $method);
}
}

View File

@@ -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'], '.');

View File

@@ -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'], '.');

View File

@@ -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',