/** * @brief 根据操作类型创建并返回dbo对象, * @param string $type 操作类型 * @return PDO */ private function createDB($type = '') { if ($type == 'SELECT' or $type == 'SHOW') { if (self::$wdb != null) { return self::$wdb; } else { if (self::$rdb != null) { return self::$rdb; } else { try { $db_config = tool::getConfig('database'); $rdb_config = $db_config['slave']; $rdb_config[] = $db_config['master']; $num = rand(0, count($rdb_config) - 1); self::$rdb = new \PDO('mysql:dbname=' . $rdb_config[$num]['database'] . ';host=' . $rdb_config[$num]['host'] . ';charset=utf8', $rdb_config[$num]['user'], $rdb_config[$num]['password']); self::$rdb->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { exit($e->getMessage()); } return self::$rdb; } } } else { if (self::$wdb != null) { return self::$wdb; } else { try { $db_config = tool::getConfig(array('database', 'master')); self::$wdb = new \PDO('mysql:dbname=' . $db_config['database'] . ';host=' . $db_config['host'] . ';charset=utf8', $db_config['user'], $db_config['password']); self::$wdb->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { exit($e->getMessage()); } return self::$wdb; } } }
/** *给定指定的模块、控制器、方法和参数列表,倒序查找application.int配置文件中的路由信息,找到匹配的路由并根据该路由规则生成url, * 目前只匹配重写和正则路由,没有匹配到的返回静态路由的结果 * @param string $controller 控制器名称 * @param string $action 方法名 * @param string $module 模块名 * @param array $params 传递的参数 * @return string */ private static function getRoute($controller, $action, $module = 'index', $params = array()) { $routes = array_reverse(tool::getConfig('routes')); //遍历路由规则配置信息 foreach ($routes as $key => $val) { if (!isset($val['route'])) { continue; } $route = $val['route']; $m = isset($route['module']) && $route['module'] != '' ? strtolower($route['module']) : 'index'; $c = isset($route['controller']) && $route['controller'] != '' ? strtolower($route['controller']) : 'index'; $a = isset($route['action']) && $route['action'] != '' ? strtolower($route['action']) : 'index'; //匹配到了参数中模块、控制器、方法的路由规则,如果规则中模块、控制器、动作带有:,则是动态匹配,表示匹配成功 //只匹配regex和rewrite两种 if (($m == $module || strpos($m, ':') !== false) && ($c == $controller || strpos($c, ':') !== false) && ($a == $action || strpos($a, ':') !== false)) { //路由匹配成功 switch ($val['type']) { case 'rewrite': //重写路由规则 $match = $val['match']; $star = strpos($match, '*') !== false ? 1 : 0; if (strpos($m, ':') !== false) { $match = str_replace($m, $module, $match); } if (strpos($c, ':') !== false) { $match = str_replace($c, $controller, $match); } if (strpos($a, ':') !== false) { $match = str_replace($a, $action, $match); } if ($star) { $match = preg_replace('/\\/\\*[\\/]?/', '', $match); foreach ($params as $k => $v) { if (strpos($match, ':' . $k) !== false) { $match = str_replace(':' . $k, $v, $match); } else { $match .= '/' . $k . '/' . $v; } } } else { foreach ($params as $k => $v) { if (strpos($match, ':' . $k) !== false) { $match = str_replace(':' . $k, $v, $match); } } } return $match; break; case 'regex': $match = $val['match']; $match = preg_replace('/^#\\^?/', '', $match); //去掉正则路由match的前导#和^ $match = preg_replace('/\\$?#$/', '', $match); //去掉最后。。。 //将match中的捕获子组(即圆括号中)替换为‘:’map名称 foreach ($val['map'] as $v) { $match = preg_replace(array('/\\([^()]*\\)/'), ':' . $v, $match, 1); } //动态模块、控制器、方法的替换 if (strpos($m, ':') !== false) { $match = str_replace($m, $module, $match); } if (strpos($c, ':') !== false) { $match = str_replace($c, $controller, $match); } if (strpos($a, ':') !== false) { $match = str_replace($a, $action, $match); } //去掉其他正则中的[]?,[]+,...,转义的字符去掉前置\ $match = preg_replace('/[\\[|\\]\\*|\\]\\+|\\]\\?]/', '', $match); $match = str_replace('\\/', '/', $match); $match = str_replace('\\?', '?', $match); //参数替换 foreach ($params as $k => $v) { if (strpos($match, ':' . $k) !== false) { $match = str_replace(':' . $k, $v, $match); } } return $match; break; } } } //没有匹配到则用yaf_route_static静态路由,/module/controller/action/parms的模式 $match = $module == 'index' ? '/' : '/' . $module; $match .= $controller . '/' . $action; foreach ($params as $key => $val) { $match .= '/' . $key . '/' . $val; } return $match; }