From 7bb958636ef75811c1146f215410918700a93b5f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 27 Mar 2015 15:38:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A0=B8=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 3 +++ convention.php | 8 +++---- library/think/app.php | 43 +++++++++++++++++++++------------ library/think/config.php | 6 +---- library/think/controller.php | 15 +++++++++++- library/think/loader.php | 1 - library/think/route.php | 46 ++++++++++++++++++------------------ library/think/template.php | 6 ++--- library/think/url.php | 31 ++++++++++-------------- library/think/view.php | 18 ++++++++++---- 10 files changed, 100 insertions(+), 77 deletions(-) diff --git a/base.php b/base.php index d4246438..b6a82832 100644 --- a/base.php +++ b/base.php @@ -29,6 +29,9 @@ defined('EXT') OR define('EXT', '.php'); defined('MODEL_LAYER') OR define('MODEL_LAYER', 'model'); defined('VIEW_LAYER') OR define('VIEW_LAYER', 'view'); defined('CONTROLLER_LAYER') OR define('CONTROLLER_LAYER', 'controller'); +defined('VAR_MODULE') OR define('VAR_MODULE', 'm'); +defined('VAR_CONTROLLER') OR define('VAR_CONTROLLER', 'c'); +defined('VAR_ACTION') OR define('VAR_ACTION', 'a'); defined('APP_DEBUG') OR define('APP_DEBUG', false); // 是否调试模式 defined('ENV_PREFIX') OR define('ENV_PREFIX', 'T_'); // 环境变量的配置前缀 diff --git a/convention.php b/convention.php index d265ac37..23d3fbd9 100644 --- a/convention.php +++ b/convention.php @@ -1,12 +1,9 @@ true, // 调试模式 'app_status' => 'debug',// 应用模式状态 - 'var_module' => 'm', // 模块变量名 - 'var_controller' => 'c', // 控制器变量名 - 'var_action' => 'a', // 操作变量名 'var_pathinfo' => 's', // PATHINFO变量名 用于兼容模式 + 'extra_config_list' => [], 'pathinfo_fetch' => 'ORIG_PATH_INFO,REDIRECT_PATH_INFO,REDIRECT_URL', 'pathinfo_depr' => '/', // pathinfo分隔符 'require_module' => true, // 是否显示模块 @@ -15,9 +12,10 @@ return [ 'default_action' => 'index', // 默认操作名 'action_suffix' => '', // 操作方法后缀 'url_model' => 1, // URL模式 + 'url_request_uri' => 'REQUEST_URI', // 获取当前页面地址的系统变量 默认为REQUEST_URI 'base_url' => $_SERVER["SCRIPT_NAME"], // 基础URL路径 'url_html_suffix' => '.html', - 'url_params_bind' => false, // url变量绑定 + 'url_params_bind' => TRUE, // url变量绑定 'exception_tmpl' => THINK_PATH.'Tpl/think_exception.tpl',// 异常页面的模板文件 'error_tmpl' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认错误跳转对应的模板文件 'success_tmpl' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认成功跳转对应的模板文件 diff --git a/library/think/app.php b/library/think/app.php index 485d8f7d..ed26729c 100644 --- a/library/think/app.php +++ b/library/think/app.php @@ -44,8 +44,9 @@ class App { // 监听app_init Hook::listen('app_init'); - define('COMMON_PATH', APP_PATH . $config['common_module'].'/'); // 初始化公共模块 + define('COMMON_PATH', APP_PATH . $config['common_module'].'/'); + self::initModule(COMMON_PATH,$config); // 应用URL调度 @@ -58,13 +59,12 @@ class App { if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测 $instance = false; }elseif($config['action_bind_class']){ - // 操作绑定到类:模块\Controller\控制器\操作 - $layer = CONTROLLER_LAYER; - if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)){ - $namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\'; + // 操作绑定到类:模块\controller\控制器\操作 + if(is_dir(MODULE_PATH.CONTROLLER_LAYER.'/'.CONTROLLER_NAME)){ + $namespace = MODULE_NAME.'\\'.CONTROLLER_LAYER.'\\'.CONTROLLER_NAME.'\\'; }else{ // 空控制器 - $namespace = MODULE_NAME.'\\'.$layer.'\\_empty\\'; + $namespace = MODULE_NAME.'\\'.CONTROLLER_LAYER.'\\empty\\'; } $actionName = strtolower(ACTION_NAME); if(class_exists($namespace.$actionName)){ @@ -161,8 +161,18 @@ class App { }else{ // 检测配置文件 if(is_file($path . 'config' . EXT)) { - $config = Config::set(include $path . 'config' . EXT); + $config = Config::set(include $path . 'config' . EXT ); } + + // 检测额外配置 + if($config['extra_config_list']){ + foreach($config['extra_config_list'] as $conf){ + if(is_file($path . $conf . EXT)) { + $config = Config::set(include $path . $conf . EXT ); + } + } + } + // 加载应用状态配置文件 if($config['app_status'] && is_file($path . $config['app_status'] . EXT)) { $config = Config::set(include $path . $config['app_status'] . EXT); @@ -197,7 +207,7 @@ class App { // 检测域名部署 if(!IS_CLI && isset($config['sub_domain_deploy']) && $config['sub_domain_deploy']) { - Route::checkDomain($config); + Route::checkDomain(); } // 监听path_info @@ -233,8 +243,8 @@ class App { } $paths = explode($config['pathinfo_depr'], __INFO__,2); // 获取URL中的模块名 - if($config['require_module'] && !isset($_GET[$config['var_module']])) { - $_GET[$config['var_module']] = array_shift($paths); + if($config['require_module'] && !isset($_GET[VAR_MODULE])) { + $_GET[VAR_MODULE] = array_shift($paths); $_SERVER['PATH_INFO'] = implode('/', $paths); } } @@ -242,6 +252,9 @@ class App { $_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix']? '/\.('.trim($config['url_html_suffix'],'.').')$/i' : '/\.'.__EXT__.'$/i', '', $_SERVER['PATH_INFO']); } + // URL常量 + define('__SELF__',strip_tags($_SERVER[$config['url_request_uri']])); + // 获取模块名称 define('MODULE_NAME', defined('BIND_MODULE')? BIND_MODULE : self::getModule($config)); @@ -257,21 +270,21 @@ class App { throw new Exception('module not exists :' . MODULE_NAME); } // 路由检测和控制器、操作解析 - Route::check($_SERVER['PATH_INFO'],$config); + Route::check($_SERVER['PATH_INFO'],$config['pathinfo_depr']); // 获取控制器名 - define('CONTROLLER_NAME', strip_tags(strtolower(isset($_GET[$config['var_controller']]) ? $_GET[$config['var_controller']] : $config['default_controller']))); + define('CONTROLLER_NAME', strip_tags(strtolower(isset($_GET[VAR_CONTROLLER]) ? $_GET[VAR_CONTROLLER] : $config['default_controller']))); // 获取操作名 - define('ACTION_NAME', strip_tags(strtolower(isset($_GET[$config['var_action']]) ? $_GET[$config['var_action']] : $config['default_action']))); + define('ACTION_NAME', strip_tags(strtolower(isset($_GET[VAR_ACTION]) ? $_GET[VAR_ACTION] : $config['default_action']))); - unset($_GET[$config['var_action']], $_GET[$config['var_controller']], $_GET[$config['var_module']]); + unset($_GET[VAR_ACTION], $_GET[VAR_CONTROLLER], $_GET[VAR_MODULE]); //保证$_REQUEST正常取值 $_REQUEST = array_merge($_POST, $_GET , $_COOKIE); } static private function getModule($config){ - $module = strtolower(isset($_GET[$config['var_module']]) ? $_GET[$config['var_module']] : $config['default_module']); + $module = strtolower(isset($_GET[VAR_MODULE]) ? $_GET[VAR_MODULE] : $config['default_module']); if($maps = $config['url_module_map']) { if(isset($maps[$module])) { // 记录当前别名 diff --git a/library/think/config.php b/library/think/config.php index 85e4d4ea..52d26af1 100644 --- a/library/think/config.php +++ b/library/think/config.php @@ -52,7 +52,7 @@ class Config { static public function get($name=null,$range='') { $range = $range ? $range : self::$range; // 无参数时获取所有 - if (empty($name)) { + if (empty($name) && isset(self::$config[$range])) { return self::$config[$range]; } $name = strtolower($name); @@ -98,8 +98,4 @@ class Config { } } - // 获取某个作用域的配置列表 - static public function getRange($rang){ - return isset(self::$config[$range]) ? self::$config[$range] : null; - } } diff --git a/library/think/controller.php b/library/think/controller.php index 2290adf2..4ac9f969 100644 --- a/library/think/controller.php +++ b/library/think/controller.php @@ -26,8 +26,9 @@ class Controller { $this->view = new View(); //控制器初始化 - if(method_exists($this, '_initialize')) + if(method_exists($this, '_initialize')){ $this->_initialize(); + } } /** @@ -114,6 +115,18 @@ class Controller { exit($data); } + /** + * Action跳转(URL重定向) 支持指定模块和延时跳转 + * @access protected + * @param string $url 跳转的URL表达式 + * @param array $params 其它URL参数 + * @return void + */ + protected function redirect($url,$params=[]) { + $url = U($url,$params); + header('Location: ' . $url); + } + /** * 操作错误跳转的快捷方法 * @access protected diff --git a/library/think/loader.php b/library/think/loader.php index 56468651..d9b0af17 100644 --- a/library/think/loader.php +++ b/library/think/loader.php @@ -10,7 +10,6 @@ // +---------------------------------------------------------------------- namespace think; -use think\Config; class Loader { // 类名映射 diff --git a/library/think/route.php b/library/think/route.php index 7a293773..20ce3fcb 100644 --- a/library/think/route.php +++ b/library/think/route.php @@ -87,7 +87,7 @@ class Route { } // 检测子域名部署 - static public function checkDomain($config=[]){ + static public function checkDomain(){ // 开启子域名部署 支持二级和三级域名 if(!empty(self::$domain)) { $rules = self::$domain; @@ -124,7 +124,7 @@ class Route { exit; } if(is_array($rule)) { - $_GET[$config['var_module']] = $rule[0]; + $_GET[VAR_MODULE] = $rule[0]; if(isset($rule[1])) { // 传入参数 parse_str($rule[1], $parms); if(isset($panDomain)) { @@ -137,19 +137,19 @@ class Route { $_GET = array_merge($_GET,$parms); } }else{ - $_GET[$config['var_module']] = $rule; + $_GET[VAR_MODULE] = $rule; } } } } // 检测URL路由 - static public function check($regx,$config) { + static public function check($regx,$depr='/') { // 优先检测是否存在PATH_INFO if(empty($regx)) $regx = '/' ; // 分隔符替换 确保路由定义使用统一的分隔符 - if('/' != $config['pathinfo_depr']){ - $regx = str_replace($config['pathinfo_depr'], '/', $regx); + if('/' != $depr){ + $regx = str_replace($depr, '/', $regx); } if(isset(self::$map[$regx])) { // URL映射 return self::parseUrl(self::$map[$regx]); @@ -190,7 +190,7 @@ class Route { self::invokeRegx($route, $matches); exit; } - return self::parseRegex($matches, $route, $regx,$config); + return self::parseRegex($matches, $route, $regx); }else{ // 规则路由 $len1 = substr_count($regx, '/'); $len2 = substr_count($rule, '/'); @@ -213,13 +213,13 @@ class Route { self::invokeRule($route, $var); exit; } - return self::parseRule($rule, $route, $regx,$config); + return self::parseRule($rule, $route, $regx); } } } } } - return self::parseUrl($regx,$config); + return self::parseUrl($regx); } /** @@ -269,22 +269,22 @@ class Route { } // 解析模块的URL地址 [模块/]控制器/操作 - static private function parseUrl($url,$config=[]) { + static private function parseUrl($url) { if('/' == $url) { return ; } $paths = explode('/', $url); - if(!defined('BIND_MODULE') && !isset($_GET[$config['var_module']])) { - $_GET[$config['var_module']] = array_shift($paths); + if(!defined('BIND_MODULE') && !isset($_GET[VAR_MODULE])) { + $_GET[VAR_MODULE] = array_shift($paths); } - if(!defined('BIND_CONTROLLER') && !isset($_GET[$config['var_controller']])) { - $_GET[$config['var_controller']] = array_shift($paths); + if(!defined('BIND_CONTROLLER') && !isset($_GET[VAR_CONTROLLER])) { + $_GET[VAR_CONTROLLER] = array_shift($paths); } if($paths){ - $_GET[$config['var_action']] = array_shift($paths); + $_GET[VAR_ACTION] = array_shift($paths); } // 解析剩余的URL参数 @@ -297,7 +297,7 @@ class Route { // 解析规范的路由地址 // 地址格式 [控制器/操作?]参数1=值1&参数2=值2... - static private function parseRoute($url,$config=[]) { + static private function parseRoute($url) { $var = []; if(false !== strpos($url, '?')) { // [控制器/操作?]参数1=值1&参数2=值2... $info = parse_url($url); @@ -310,12 +310,12 @@ class Route { } if(isset($path)) { $action = array_pop($path); - $_GET[$config['var_action']] = '[rest]'==$action ? REQUEST_METHOD : $action; + $_GET[VAR_ACTION] = '[rest]'==$action ? REQUEST_METHOD : $action; if(!empty($path)) { - $_GET[$config['var_controller']] = array_pop($path); + $_GET[VAR_CONTROLLER] = array_pop($path); } if(!empty($path)) { - $_GET[$config['var_module']] = array_pop($path); + $_GET[VAR_MODULE] = array_pop($path); } } return $var; @@ -361,7 +361,7 @@ class Route { // 外部地址中可以用动态变量 采用 :1 :2 的方式 // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'), // 'new/:id'=>array('/new.php?id=:1',301), 重定向 - static private function parseRule($rule, $route, $regx,$config) { + static private function parseRule($rule, $route, $regx) { // 获取路由地址规则 $url = is_array($route) ? $route[0] : $route; // 获取URL地址中的参数 @@ -392,7 +392,7 @@ class Route { exit; }else{ // 解析路由地址 - $var = self::parseRoute($url,$config); + $var = self::parseRoute($url); // 解析路由地址里面的动态参数 $values = array_values($matches); foreach ($var as $key => $val){ @@ -424,7 +424,7 @@ class Route { // 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式 // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'), // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向 - static private function parseRegex($matches, $route, $regx,$config) { + static private function parseRegex($matches, $route, $regx) { // 获取路由地址规则 $url = is_array($route) ? $route[0] : $route; $url = preg_replace('/:(\d+)/e', '$matches[\\1]', $url); @@ -433,7 +433,7 @@ class Route { exit; }else{ // 解析路由地址 - $var = self::parseRoute($url,$config); + $var = self::parseRoute($url); // 解析剩余的URL参数 $regx = substr_replace($regx, '', 0, strlen($matches[0])); if($regx) { diff --git a/library/think/template.php b/library/think/template.php index 9daff0b1..6b24f818 100644 --- a/library/think/template.php +++ b/library/think/template.php @@ -50,10 +50,8 @@ class Template { * 架构函数 * @access public */ - public function __construct($config=[]){ - if(!empty($config)) { - $this->config = array_merge($this->config,$config); - } + public function __construct(array $config=[]){ + $this->config = array_merge($this->config, empty($config) ? (array)Config::get('template') : $config ); $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); diff --git a/library/think/url.php b/library/think/url.php index 72e504a8..df4dbfac 100644 --- a/library/think/url.php +++ b/library/think/url.php @@ -13,14 +13,6 @@ namespace think; class Url { - static public function param($num,$default=''){ - $paths = explode(Config::get('url_pathinfo_depr'),trim($_SERVER['PATH_INFO'],'/')); - return isset($paths[$num])?$paths[$num]:$default; - } - - static public function route($route){ - } - /** * URL组装 支持不同URL模式 * @param string $url URL表达式,格式:'[分组/模块/操作#锚点@域名]?参数1=值1&参数2=值2...' @@ -30,6 +22,7 @@ class Url { * @return string */ static public function build($url='',$vars='',$suffix=true,$domain=false) { + $config = Config::get(); // 解析URL $info = parse_url($url); $url = !empty($info['path'])?$info['path']:ACTION_NAME; @@ -49,10 +42,10 @@ class Url { $domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.')); }elseif($domain===true){ $domain = $_SERVER['HTTP_HOST']; - if(Config::get('app_sub_domain_deplay') ) { // 开启子域名部署 + if($config['app_sub_domain_deplay'] ) { // 开启子域名部署 $domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.'); // '子域名'=>array('项目[/分组]'); - foreach (Config::get('app_sub_domain_rules') as $key => $rule) { + foreach ($config['app_sub_domain_rules'] as $key => $rule) { if(false === strpos($key,'*') && 0=== strpos($url,$rule[0])) { $domain = $key.strstr($domain,'.'); // 生成对应子域名 $url = substr_replace($url,'',0,strlen($rule[0])); @@ -74,7 +67,7 @@ class Url { } // URL组装 - $depr = Config::get('pathinfo_depr'); + $depr = $config['pathinfo_depr']; if($url) { if(0=== strpos($url,'/')) {// 定义路由 $route = true; @@ -90,27 +83,27 @@ class Url { $url = trim($url,$depr); $path = explode($depr,$url); $var = []; - $var[Config::get('var_action')] = !empty($path)?array_pop($path):ACTION_NAME; + $var[VAR_ACTION] = !empty($path)?array_pop($path):ACTION_NAME; if(!defined('BIND_CONTROLLER')){ - $var[Config::get('var_controller')] = !empty($path)?array_pop($path):CONTROLLER_NAME; + $var[VAR_CONTROLLER] = !empty($path)?array_pop($path):CONTROLLER_NAME; } if(!defined('BIND_MODULE')){ - $var[Config::get('var_module')] = !empty($path)?array_pop($path):MODULE_NAME; + $var[VAR_MODULE] = !empty($path)?array_pop($path):MODULE_NAME; } } } - if(Config::get('url_model') == 0) { // 普通模式URL转换 - $url = Config::get('base_url').'?'.http_build_query(array_reverse($var)); + if($config['url_model'] == 0) { // 普通模式URL转换 + $url = $config['base_url'].'?'.http_build_query(array_reverse($var)); if(!empty($vars)) { $vars = urldecode(http_build_query($vars)); $url .= '&'.$vars; } }else{ // PATHINFO模式或者兼容URL模式 if(isset($route)) { - $url = Config::get('base_url').'/'.rtrim($url,$depr); + $url = $config['base_url'].'/'.rtrim($url,$depr); }else{ - $url = Config::get('base_url').'/'.implode($depr,array_reverse($var)); + $url = $config['base_url'].'/'.implode($depr,array_reverse($var)); } if(!empty($vars)) { // 添加参数 foreach ($vars as $var => $val){ @@ -118,7 +111,7 @@ class Url { } } if($suffix) { - $suffix = $suffix===true?Config::get('url_html_suffix'):$suffix; + $suffix = $suffix===true?$config['url_html_suffix']:$suffix; if($pos = strpos($suffix, '|')){ $suffix = substr($suffix, 0, $pos); } diff --git a/library/think/view.php b/library/think/view.php index 8c42b684..30f29d6b 100644 --- a/library/think/view.php +++ b/library/think/view.php @@ -10,7 +10,6 @@ // +---------------------------------------------------------------------- namespace think; -use think\Exception; class View { protected $engine = null; // 模板引擎实例 @@ -34,6 +33,11 @@ class View { 'engine_type' => 'think', ]; + public function __construct(array $config = []){ + $this->config = array_merge($this->config, empty($config)? (array)Config::get('view') : $config); + $this->engine($this->config['engine_type']); + } + /** * 模板变量赋值 * @access public @@ -59,11 +63,17 @@ class View { $this->config[$name] = $value; } - public function __construct(array $config = []){ + /** + * 设置视图参数 + * @access public + * @param array $config 视图参数 + * @return View + */ + public function config(array $config=[]){ $this->config = array_merge($this->config, $config); - $this->engine($this->config['engine_type']); + return $this; } - + /** * 设置当前模板解析的引擎 * @access public