/** * 根据URL分发到Controller和Model * @access public * @param array $url_array */ public static function routeToCm($url_array = array()) { $app = ''; $controller = ''; $action = ''; $model = ''; $params = ''; self::$_urlparams = $url_array; //带有文件夹的model和controller,比如后台admin等 if (isset($url_array['app'])) { $app = $url_array['app']; } $controller = self::$_config['route']['default_controller']; //controller 只有一个 if (isset($url_array['r'])) { $model = isset($url_array['r']) ? $url_array['r'] : self::$_config['route']['default_controller']; if ($app) { $controller_file = CONTROLLER_PATH . '/' . $app . '/' . $controller . 'Controller.php'; $model_file = MODEL_PATH . '/' . $app . '/' . $model . 'Model.php'; } else { $controller_file = CONTROLLER_PATH . '/' . $controller . 'Controller.php'; $model_file = MODEL_PATH . '/' . $model . 'Model.php'; } } else { if ($app) { $controller_file = CONTROLLER_PATH . '/' . $app . '/' . self::$_config['route']['default_controller'] . 'Controller.php'; $model_file = MODEL_PATH . '/' . $app . '/' . self::$_config['route']['default_controller'] . 'Model.php'; } else { $controller_file = CONTROLLER_PATH . '/' . self::$_config['route']['default_controller'] . 'Controller.php'; $model_file = MODEL_PATH . '/' . self::$_config['route']['default_controller'] . 'Model.php'; } } /** * 获取spider配置,并替换默认配置 * 如果存在则替换,如果不存在则终止程序的执行 */ $spider_file = Spider_Conf_PATH . '/' . $model . 'Spider.php'; if (file_exists($spider_file)) { require $spider_file; //若存在特定的数据库配置则更新,否则使用默认的数据库配置 if (isset($siteconfig['db'])) { if (isset($siteconfig['db']['mongodb'])) { self::$_config['db']['mongodb'] = $siteconfig['db']['mongodb']; } if (isset($siteconfig['db']['mongodbsec'])) { self::$_config['db']['mongodbsec'] = $siteconfig['db']['mongodbsec']; } if (isset($siteconfig['db']['redis'])) { self::$_config['db']['redis'] = $siteconfig['db']['redis']; } if (isset($siteconfig['db']['mysql'])) { self::$_config['db']['mysql'] = $siteconfig['db']['mysql']; } } self::$_spider = $siteconfig; self::$_spidername = $model; //传入进程数 if (isset($url_array['process']) && $url_array['process'] > 0 && $url_array['a'] == 'fulldata') { self::$_process = $url_array['process']; } if (isset($url_array['process']) && $url_array['process'] > 0 && strpos($url_array['a'], 'master')) { self::$_process = $url_array['process']; } } else { echo $model . "'s Spider doesn't exist."; exit; } /** * 加载后处理模块 */ $product_file = MODEL_PATH . '/' . $model . 'ProductModel.php'; if (file_exists($product_file)) { require $product_file; } else { echo $model . "'s ProductModel doesn't exist."; exit; } if (isset($url_array['a'])) { $action = $url_array['a']; } else { $action = self::$_config['route']['default_action']; } /** * 判断扩展的model是否存在,如果存在则调用,否则视为默认的 */ if (isset($url_array['params'])) { $params = $url_array['params']; } if (file_exists($controller_file)) { if (file_exists($model_file)) { require $model_file; self::$_spidermodel = $model; } require $controller_file; $controller = $controller . 'Controller'; $controller = new $controller(); /** * 控制器、方法校验 */ if ($action && method_exists($controller, $action)) { isset($params) ? $controller->{$action}($params) : $controller->{$action}(); } else { echo '控制器方法不存在'; // die('控制器方法不存在'); } } else { die('默认控制器不存在'); } }