主要是根据控制器文件生成规则,并批量插入到数据库,说实话,手动一个一个写的话有点慢:

主要的代码:
<?php
namespace Admin\Controller;
use Think\Controller;
Class FileController extends Controller{
Public function index(){
// 设置控制器的目录
$root_path = APP_PATH . MODULE_NAME . '/Controller/';
$arr = array();
// 循环控制器文件
foreach (scandir($root_path) as $path) {
if ($path == '.' || $path == '..') continue;
if (pathinfo($root_path . $path, PATHINFO_EXTENSION) != 'php') continue;
// 解析控制器文件
$handle = @fopen($root_path . $path, "r");
$actionArr = array();
$controllers = array();
$actions = array();
while(!feof($handle)) {
$buffer = fgets($handle);
// $controllers[1] 是获取的控制器名称
if (!$controllers[1]) {
preg_match('/\s+([a-zA-Z]+)Controller\s+extends/', $buffer, $controllers);
}
preg_match('/function\s+([_a-zA-Z]+)\s*[(]/', $buffer, $actions);
// $actions[1] 是获取的方法名
if ($actions[1]) {
// 生成规则
$actionArr[] = MODULE_NAME . '/'. $controllers[1] . '/' . $actions[1];
}
}
fclose($handle);
$arr[basename($root_path . $path)] = $actionArr;
}
$this->assign('arr',$arr);
$this->display();
}
} 最佳答案