public function __construct($argv = array()) { if (!defined("PI_APP_NAME")) { die("app.err please define PI_APP_NAME const \n"); } $this->mode = 'api'; $this->app_env = Pi::get('app_env', ''); $this->com_env = Pi::get('com_env', ''); $data_type = Pcf::get("global.data_type", 'json'); if (isset($this->data_types[$data_type])) { $this->data_type = $data_type; } parent::__construct(); if (true === $this->debug && php_sapi_name() == 'cli') { $mod_name = Pcf::get("global.mod", 'mod'); $func_name = Pcf::get("global.func", 'func'); Comm::setReq($mod_name, $argv[1]); Comm::setReq($func_name, $argv[2]); } }
public function customRouter($url = null, $domain = '') { $this->setBasePath(APP_CTR_ROOT); $uri = $url == null ? $this->uri : $url; if (empty($uri)) { return false; } //去掉空path $tmp_uri = explode('/', $uri); $uri = array(); //去掉空元素 if (!empty($tmp_uri)) { foreach ($tmp_uri as $v) { if (!empty($v)) { $uri[] = $v; } } } //附加二级域名处理 $host = explode('.', $this->host); $this->host_pre = empty($host) ? 'www' : $host[0]; if (is_string($domain) && !empty($domain)) { $this->host_pre = $domain; } if ($this->host_pre == 'ajax') { array_unshift($uri, 'ajax'); } if (isset($uri[0]) && $uri[0] == 'ajax') { $this->setBasePath(APP_CTR_ROOT . 'ajax' . DOT); array_shift($uri); $this->class_pre = 'Ajax'; } else { $this->class_pre = ''; } if (Pcf::get('route.second_domain_enable', true)) { $base = Pcf::get("route.alias_" . $this->host_pre, $this->host_pre); if (!empty($base) && $this->host_pre != 'www' && $this->host_pre != 'ajax') { array_unshift($uri, $base); } } $uri = implode('/', $uri); //检查uri安全性 if (!preg_match('/^[\\+\\-_0-9a-zA-Z\\/]*$/', $uri)) { throw new Exception('router.err illegal uri visit ' . $uri, 1027); } //处理自定义路由,支持目前只匹配带有数字,数字字母混合参数的uri和绝对跳转 $self_router = Pcf::get('route.custom_router'); //绝对跳转,没有参数在path_info if (isset($self_router[$uri])) { $uri = $self_router[$uri]; } else { $param_pattern = '/[\\+\\-_0-9]+/'; if (preg_match($param_pattern, $uri)) { //带参转换 $tmp_mod = explode('/', $uri); $guess_router = ''; $params = array(); if (!empty($tmp_mod)) { foreach ($tmp_mod as $p) { if (preg_match($param_pattern, $p)) { $params[] = $p; $p = '(:p)'; } $guess_router .= $p . '/'; } $guess_router = rtrim($guess_router, '/'); if (isset($self_router[$guess_router])) { $uri = $self_router[$guess_router]['url']; $self_params = $self_router[$guess_router]['p']; if (!empty($self_params)) { foreach ($self_params as $pn) { Comm::setReq($pn, array_shift($params)); } } } } } } //处理完自定义路由后防止内部调用dispath循环跳转。如果有发现问题,跳转到主页 $this->uri = $url != null && $uri == $this->uri ? '' : $uri; return $this->uri; }