Yii2 获取所有action

   public function actionTest()
    {
        $basePath = dirname(Yii::$app->BasePath);
        // 所有控制器文件夹的绝对路径
        $module_path = $basePath . '/backend/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;
            }
            // 控制器中驼峰式命名中的大写转为-小写
            $files = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-$1', basename($value, 'Controller.php')));
            // 读取控制器文件内容
            $file_arr = file($value);
            // 逐行读取
            for($i = 0, $iMax = count($file_arr); $i < $iMax; $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][] = $item = "/$files/".strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-$1', substr($content,$start+14,$end-$length)));
                        $time = time();
                        $sql = "insert ignore into jc_auth_item (`name`, `type`, `created_at`, `updated_at`) VALUES ('$item', 2, $time, $time) ;";
                        file_put_contents('auth_item.sql', $sql . PHP_EOL, FILE_APPEND);
                    }
                }
            }
            $authItems[$files][] = $item = "/$files/*";
            $sql = "insert ignore into jc_auth_item (`name`, `type`, `created_at`, `updated_at`) VALUES ('$item', 2, $time, $time) ;";
            file_put_contents('auth_item.sql', $sql . PHP_EOL, FILE_APPEND);
        }
        var_dump($authItems);
//        return $authItems;
    }

你可能感兴趣的:(Yii2 获取所有action)