下面這個方法是起到路由作用的,它是怎麼完成這個流程的
/**
* 控制器調度
*
*/
private static function control(){
//次層網域
//var_dump($GLOBALS['setting_config']['enabled_subdomain']);
if ($GLOBALS['setting_config']['enabled_subdomain'] == '1' && $_GET['act'] == 'index' && $_GET['op'] == 'index'){
$store_id = subdomain();
if ($store_id > 0) $_GET['act'] = 'show_store';
}
$act_file = realpath(BASE_PATH.'/control/'.$_GET['act'].'.php');
$class_name = $_GET['act'].'Control';
//echo $act_file;
if ([email protected]($act_file)){
if (C('debug')) {
throw_exception("Base Error: access file isn't exists!");
} else {
showMessage('抱歉!您訪問的頁面不存在','','html','error');
}
}
if (class_exists($class_name)){
$main = new $class_name();
$function = $_GET['op'].'Op';
if (method_exists($main,$function)){
$main->$function();
}elseif (method_exists($main,'indexOp')){
$main->indexOp();
}else {
$error = "Base Error: function $function not in $class_name!";
throw_exception($error);
}
}else {
$error = "Base Error: class $class_name isn't exists!";
throw_exception($error);
}
}
------解決思路----------------------
根據controler與action,判斷要調用的類與方法
$act_file = realpath(BASE_PATH.'/control/'.$_GET['act'].'.php');
$class_name = $_GET['act'].'Control';
然後判斷如果類和方法存在則調用,就這樣。
if (class_exists($class_name)){
$main = new $class_name();
$function = $_GET['op'].'Op';
if (method_exists($main,$function)){
$main->$function();
}elseif (method_exists($main,'indexOp')){
$main->indexOp();
}else {
$error = "Base Error: function $function not in $class_name!";
throw_exception($error);
}
}