Yii获取所有控制器和方法
public function actionTest()
{
$basePath = dirname(Yii::$app->BasePath);
// 项目文件夹名
$project = substr(Yii::$app->id,(strpos(Yii::$app->id,'-') + 1));
// 所有控制器文件夹的绝对路径
$module_path = $basePath . '/' . $project . '/controllers/';
if(!is_dir($module_path)) {
return 'error';
}
$module_path .= '*Controller.php';
// 所有控制器文件的绝对路径
$allFile = glob($module_path);
$authItems = [];
foreach ($allFile as $key => $value) {
if (is_dir($value) && !file_exists($value)) {
continue;
} else {
// 控制器中驼峰式命名中的大写转为-小写
$files = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-$1', basename($value, 'Controller.php')));
// 读取控制器文件内容
$file_arr = file($value);
// 逐行读取
for($i = 0; $i < count($file_arr); $i ++) {
// 转译html标签
$content = htmlspecialchars($file_arr[$i]);
if (!empty($content)) {
// 删除所有空格
$content = str_replace(' ','',$content);
// 整行代码长度
$length = strlen($content);
// 方法名的起始位置
$start = strpos($content, 'functionaction');
// 方法名的结束位置
$end = strpos($content, '()');
if ($start && $end && $length) {
// 方法中驼峰式命名中的大写转为-小写
$authItems[$files][] = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-$1', substr($content,$start+14,$end-$length)));
}
}
}
}
}
echo '<pre>';print_r($authItems);exit;
return $authItems;
}
Yii获取所有控制器和方法
最新推荐文章于 2021-03-18 23:33:38 发布