shopnc路由功能分析

项目核心文件

core/shopld.php

 1 if (!@include(BASE_DATA_PATH.'/config/config.ini.php')) exit('config.ini.php isn\'t exists!');
 2 if (file_exists(BASE_PATH.'/config/config.ini.php')){
 3     include(BASE_PATH.'/config/config.ini.php');
 4 }
 5 global $config;
 6 
 7 //默认平台店铺id
 8 define('DEFAULT_PLATFORM_STORE_ID', $config['default_store_id']);
 9 
10 define('URL_MODEL',$config['url_model']);
11 define(SUBDOMAIN_SUFFIX, $config['subdomain_suffix']);
12 
13 define('SHOP_SITE_URL', $config['shop_site_url']);
14 define('CMS_SITE_URL', $config['cms_site_url']);
15 define('CIRCLE_SITE_URL', $config['circle_site_url']);
16 define('MICROSHOP_SITE_URL', $config['microshop_site_url']);
17 define('ADMIN_SITE_URL', $config['admin_site_url']);
18 define('MOBILE_SITE_URL', $config['mobile_site_url']);
19 define('WAP_SITE_URL', $config['wap_site_url']);
20 define('UPLOAD_SITE_URL',$config['upload_site_url']);
21 define('RESOURCE_SITE_URL',$config['resource_site_url']);
22 
23 define('BASE_DATA_PATH',BASE_ROOT_PATH.'/data');
24 define('BASE_UPLOAD_PATH',BASE_DATA_PATH.'/upload');
25 define('BASE_RESOURCE_PATH',BASE_DATA_PATH.'/resource');
26 
27 define('CHARSET',$config['db'][1]['dbcharset']);
28 define('DBDRIVER',$config['dbdriver']);
29 define('SESSION_EXPIRE',$config['session_expire']);
30 define('LANG_TYPE',$config['lang_type']);
31 define('COOKIE_PRE',$config['cookie_pre']);
32 define('DBPRE',($config['db'][1]['dbname']).'`.`'.($config['tablepre']));
33 
34 $_GET['act'] = $_GET['act'] ? strtolower($_GET['act']) : ($_POST['act'] ? strtolower($_POST['act']) : null);
35 $_GET['op'] = $_GET['op'] ? strtolower($_GET['op']) : ($_POST['op'] ? strtolower($_POST['op']) : null);
36 
37 if (empty($_GET['act'])){
38     require_once(BASE_CORE_PATH.'/framework/core/route.php');
39     new Route($config);
40 }
41 //统一ACTION
42 $_GET['act'] = preg_match('/^[\w]+$/i',$_GET['act']) ? $_GET['act'] : 'index';
43 $_GET['op'] = preg_match('/^[\w]+$/i',$_GET['op']) ? $_GET['op'] : 'index';
44 
45 //对GET POST接收内容进行过滤,$ignore内的下标不被过滤
46 $ignore = array('article_content','pgoods_body','doc_content','content','sn_content','g_body','store_description','p_content','groupbuy_intro','remind_content','note_content','ref_url','adv_pic_url','adv_word_url','adv_slide_url','appcode','mail_content');
47 if (!class_exists('Security')) require(BASE_CORE_PATH.'/framework/libraries/security.php');
48 $_GET = !empty($_GET) ? Security::getAddslashesForInput($_GET,$ignore) : array();
49 $_POST = !empty($_POST) ? Security::getAddslashesForInput($_POST,$ignore) : array();
50 $_REQUEST = !empty($_REQUEST) ? Security::getAddslashesForInput($_REQUEST,$ignore) : array();
51 $_SERVER = !empty($_SERVER) ? Security::getAddSlashes($_SERVER) : array();
52 
53 //启用ZIP压缩
54 if ($config['gzip'] == 1 && function_exists('ob_gzhandler') && $_GET['inajax'] != 1){
55     ob_start('ob_gzhandler');
56 }else {
57     ob_start();
58 }
59 
60 require_once(BASE_CORE_PATH.'/framework/function/core.php');
61 require_once(BASE_CORE_PATH.'/framework/core/base.php');
62 
63 require_once(BASE_CORE_PATH.'/framework/function/goods.php');
64 
65 if(function_exists('spl_autoload_register')) {
66     spl_autoload_register(array('Base', 'autoload'));
67 } else {
68     function __autoload($class) {
69         return Base::autoload($class);
70     }
71 }

 

 

 

 

路由解析文件位于core/framework/core/route.php 主要控制路由功能和伪静态功能

  

  1 <?php
  2 /**
  3  * 路由
  4  *
  5  * @copyright  Copyright (c) 2014-2015 shopld Inc. 
  6  * @license    
  7  * @link       
  8  * @since      File available since Release v1.0
  9  */
 10 defined('InSC') or exit('Access Invalid!');
 11 
 12 class Route {
 13 
 14     /**
 15      * PATH_INFO 分隔符
 16      * 
 17      * @var string
 18      */
 19     private $_pathinfo_split = '-';
 20 
 21     /**
 22      * 系统配置信息
 23      * 
 24      * @var array
 25      */
 26 
 27     private $_config = array();
 28 
 29     /**
 30      * PATH_INFO内容分隔正则
 31      *
 32      * @var string
 33      */
 34     private $_pathinfo_pattern = '';
 35 
 36     /**
 37      * 伪静态文件扩展名
 38      *
 39      * @var string
 40      */
 41     private $_rewrite_extname = '.html';
 42 
 43     /**
 44      * 构造方法
 45      *
 46      */
 47     public function __construct($config = array()) {
 48         $this->_config = $config;
 49         $this->_pathinfo_pattern = "/{$this->_pathinfo_split}/";
 50         $this->parseRule();
 51     }
 52 
 53     /**
 54      * 路由解析
 55      *
 56      */
 57     public function parseRule() {
 58         if ($this->_config['url_model']) {
 59             $this->_parseRuleRewrite();
 60         } else {
 61             $this->_parseRuleNormal();
 62         }
 63     }
 64 
 65     /**
 66      * 默认URL模式
 67      *
 68      */
 69     private function _parseRuleNormal() {
 70         //不进行任何处理
 71     }
 72 
 73     /**
 74      * 伪静态模式
 75      *
 76      */
 77     private function _parseRuleRewrite() {
       //$_SERVER['path_info']包含真实脚本名称之后,并且在查询语句之前的信息,如果当前脚本是通过 URL http://www.example.com/php/path_info.php/some/stuff?foo=bar 被访问,那么 $_SERVER['PATH_INFO'] 将包含 /some/stuff。  
       //$_SERVER['REDIRECT_REDIRECT_PATH_INFO']     
78 $path_info = !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $_SERVER['REDIRECT_REDIRECT_PATH_INFO']; 79 $path_info = trim($path_info,'/'); 80 81 //如果为二级目录去掉目录信息 82 if(strpos($path_info, '/')) { 83 $path_info_array = explode('/', $path_info); 84 $path_info = end($path_info_array); 85 } 86 87 if (!empty($path_info) && $path_info != 'index.php' && strpos($path_info, $this->_rewrite_extname)){ 88 //去掉伪静态扩展名 89 $path_info = substr($path_info,0,-strlen($this->_rewrite_extname)); 90 91 //根据不同APP匹配url规则 92 $path_info_function = '_' . APP_ID . 'PathInfo'; 93 $path_info = $this->$path_info_function($path_info); 94 95 $split_array = preg_split($this->_pathinfo_pattern,$path_info); 96 //act,op强制赋值 97 $_GET['act'] = isset($split_array[0]) ? $split_array[0] : 'index'; 98 $_GET['op'] = isset($split_array[1]) ? $split_array[1] : 'index'; 99 unset($split_array[0]); 100 unset($split_array[1]); 101 102 //其它参数也放入$_GET 103 while (current($split_array) !== false) { 104 $name = current($split_array); 105 $value = next($split_array); 106 $_GET[$name] = $value; 107 if (next($split_array) === false){ 108 break; 109 } 110 } 111 } else { 112 $_GET['act'] = $_GET['op'] = 'index'; 113 } 114 } 115 116 /** 117 * 商城短网址还原成长网址 118 * @param unknown $path_info 119 * @return mixed 120 */ 121 private function _shopPathInfo($path_info) { 122 $reg_match_from = array( 123 '/^item-(\d+)$/', 124 '/^shop-(\d+)$/', 125 '/^shop_view-(\d+)-(\d+)-([0-5])-([0-2])-(\d+)$/', 126 '/^article-(\d+)$/', 127 '/^article_cate-(\d+)$/', 128 '/^document-([a-z_]+)$/', 129 '/^cate-(\d+)-([0-9_]+)-([0-9_]+)-([0-3])-([0-2])-([0-2])-(\d+)-(\d+)$/', 130 '/^brand-(\d+)-([0-3])-([0-2])-([0-2])-(\d+)-(\d+)$/', 131 '/^brand$/', 132 '/^groupbuy-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)$/', 133 '/^groupbuy_soon-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)$/', 134 '/^groupbuy_history-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)-(\d+)$/', 135 '/^groupbuy_detail-(\d+)$/', 136 '/^integral$/', 137 '/^integral_list$/', 138 '/^integral_item-(\d+)$/', 139 '/^voucher$/', 140 '/^comments-(\d+)-([0-3])-(\d+)$/', 141 '/^special-(\d+)$/' 142 ); 143 $reg_match_to = array( 144 'goods-index-goods_id-\\1', 145 'show_store-index-store_id-\\1', 146 'show_store-goods_all-store_id-\\1-stc_id-\\2-key-\\3-order-\\4-curpage-\\5', 147 'article-show-article_id-\\1', 148 'article-article-ac_id-\\1', 149 'document-index-code-\\1', 150 'search-index-cate_id-\\1-b_id-\\2-a_id-\\3-key-\\4-order-\\5-type-\\6-area_id-\\7-curpage-\\8', 151 'brand-list-brand-\\1-key-\\2-order-\\3-type-\\4-area_id-\\5-curpage-\\6', 152 'brand-index', 153 'show_groupbuy-index-area_id-\\1-groupbuy_class-\\2-groupbuy_price-\\3-groupbuy_order_key-\\4-groupbuy_order-\\5-curpage-\\6', 154 'show_groupbuy-groupbuy_soon-area_id-\\1-groupbuy_class-\\2-groupbuy_price-\\3-groupbuy_order_key-\\4-groupbuy_order-\\5-curpage-\\6', 155 'show_groupbuy-groupbuy_history-area_id-\\1-groupbuy_class-\\2-groupbuy_price-\\3-groupbuy_order_key-\\4-groupbuy_order-\\5-curpage-\\6', 156 'show_groupbuy-groupbuy_detail-group_id-\\1', 157 'pointprod-index', 158 'pointprod-plist', 159 'pointprod-pinfo-id-\\1', 160 'pointvoucher-index', 161 'goods-comments_list-goods_id-\\1-type-\\2-curpage-\\3', 162 'special-special_detail-special_id-\\1' 163 ); 164 return preg_replace($reg_match_from,$reg_match_to,$path_info); 165 } 166 167 /** 168 * CMS短网址还原成长网址 169 * @param unknown $path_info 170 * @return mixed 171 */ 172 private function _cmsPathInfo($path_info) { 173 $reg_match_from = array( 174 '/^article-(\d+)$/', 175 '/^picture-(\d+)$/' 176 ); 177 $reg_match_to = array( 178 'article-article_detail-article_id-\\1', 179 'picture-picture_detail-picture_id-\\1' 180 ); 181 return preg_replace($reg_match_from,$reg_match_to,$path_info); 182 } 183 184 }

 

posted @ 2015-04-07 08:49  gophper  阅读(2440)  评论(0编辑  收藏  举报