Home  >  Article  >  Backend Development  >  下面这个方法是起到路由作用的,它是如何完成这个流程的

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

WBOY
WBOYOriginal
2016-06-13 12:20:131001browse

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

/**<br />	 * 控制器调度<br />	 *<br />	 */<br />	private static function control(){<br />		//二级域名<br />            //var_dump($GLOBALS['setting_config']['enabled_subdomain']);<br />		if ($GLOBALS['setting_config']['enabled_subdomain'] == '1' && $_GET['act'] == 'index' && $_GET['op'] == 'index'){<br />			$store_id = subdomain();<br />			if ($store_id > 0) $_GET['act'] = 'show_store';<br />		}<br />		$act_file = realpath(BASE_PATH.'/control/'.$_GET['act'].'.php');<br />		$class_name = $_GET['act'].'Control';<br />                //echo $act_file;<br />		if ([email&#160;protected]($act_file)){<br />		    if (C('debug')) {<br />		        throw_exception("Base Error: access file isn't exists!");<br />		    } else {<br />		        showMessage('抱歉!您访问的页面不存在','','html','error');<br />		    }<br />		}<br />		if (class_exists($class_name)){<br />			$main = new $class_name();<br />			$function = $_GET['op'].'Op';<br />			if (method_exists($main,$function)){<br />				$main->$function();<br />			}elseif (method_exists($main,'indexOp')){<br />				$main->indexOp();<br />			}else {<br />				$error = "Base Error: function $function not in $class_name!";<br />				throw_exception($error);<br />			}<br />		}else {<br />			$error = "Base Error: class $class_name isn't exists!";<br />			throw_exception($error);<br />		}<br />	}

------解决思路----------------------
根据controler与action,判断要调用的类与方法
<br />$act_file = realpath(BASE_PATH.'/control/'.$_GET['act'].'.php');<br />$class_name = $_GET['act'].'Control';<br />

然后判断如果类和方法存在则调用,就这样。
<br />if (class_exists($class_name)){<br />            $main = new $class_name();<br />            $function = $_GET['op'].'Op';<br />            if (method_exists($main,$function)){<br />                $main->$function();<br />            }elseif (method_exists($main,'indexOp')){<br />                $main->indexOp();<br />            }else {<br />                $error = "Base Error: function $function not in $class_name!";<br />                throw_exception($error);<br />            }<br />        }<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn