通过控制器实现类似Java的工厂模式
在主入口文件中通过DIRECTORY_SEPARATOR和dirname(__FILE__)以及url中的参数拼接控制模块文件,通过include 进入子控制模块
$l = DIRECTORY_SEPARATOR;
$dir = dirname(__FILE__);
$ctrlName = trim($operation);
$controllerFile = "{$dir}{$l}目录名{$l}xxx{$ctrlName}.php";
if (file_exists($controllerFile)) {
include $controllerFile;
}else {
aaa(-1,lang ( "message", "undefined_action" ));
}
在子控制模块文件中判断逻辑返回json字符串
function aaa($result, $message, $data = array()) {
$jdata['result'] = $result;
$jdata['message'] = $message;
if(!empty($data)){
$jdata['data'] = $data;
}
echo json_encode($jdata);
exit;
}