Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving array of Controllers/Actions

Tags:

yii2

Is it possible in Yii2 to retrieve an array containing all controllers and actions for the whole application?

like image 384
Andreas Hinderberger Avatar asked Jan 13 '15 00:01

Andreas Hinderberger


1 Answers

I finally ended up with:

protected function actionGetcontrollersandactions()
{
    $controllerlist = [];
    if ($handle = opendir('../controllers')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && substr($file, strrpos($file, '.') - 10) == 'Controller.php') {
                $controllerlist[] = $file;
            }
        }
        closedir($handle);
    }
    asort($controllerlist);
    $fulllist = [];
    foreach ($controllerlist as $controller):
        $handle = fopen('../controllers/' . $controller, "r");
        if ($handle) {
            while (($line = fgets($handle)) !== false) {
                if (preg_match('/public function action(.*?)\(/', $line, $display)):
                    if (strlen($display[1]) > 2):
                        $fulllist[substr($controller, 0, -4)][] = strtolower($display[1]);
                    endif;
                endif;
            }
        }
        fclose($handle);
    endforeach;
    return $fulllist;
}
like image 69
Andreas Hinderberger Avatar answered Nov 16 '22 02:11

Andreas Hinderberger