获取模块中所有控制器:
private function getControllers($module){
if(empty($module)) {
return null;
}
$modulePath = app()->getAppPath() . $module . '/controller/';
if(!is_dir($modulePath)) {
return null;
}
$modulePath .= '*.php';
$matchFiles = glob($modulePath);
$files = [];
foreach ($matchFiles as $file) {
if(is_dir($file)) {
continue;
} else {
$files[] = basename($file, '.php');
}
}
return $files;
}
获取控制器所有操作:
private function getActions($module, $controller){
if(empty($controller)) {
return null;
}
$customerFunctions = [];
$file = app()->getAppPath() . $module . '/controller/' . $controller . '.php';
if(file_exists($file)) {
$content = file_get_contents($file);
preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
$functions = $matches[1];
$excludeFunc = ['__initialize', '__construct'];
foreach ($functions as $func) {
$func = trim($func);
if(!in_array($func, $excludeFunc)) {
$customerFunctions[] = $func;
}
}
return $customerFunctions;
} else {
Log::record('is not file: ' . $file);
return false;
}
}
把所有需要的模块列出来,循环获取控制器,每个控制器循环获取里面的操作,再拼接成路由