首页 >后端开发 >php教程 >下面这个方法是起到路由作用的,它是怎么完成这个流程的

下面这个方法是起到路由作用的,它是怎么完成这个流程的

WBOY
WBOY原创
2016-06-20 12:55:56815浏览

/**	 * 控制器调度	 *	 */	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 (!@include($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);            }        }

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn