Retrieving an array of controllers / actions

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

+5
source share
4 answers

I finally finished:

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; } 
+4
source

As far as I know, Yii 2 has no built-in methods to achieve this. You can get only the current controller and its action.

What is the purpose of this? If you really need it, you can write this functionality yourself.

To get all the controllers, you should look for files ending in Conroller . And they can be located in different places of application. For example, in subfolders, modules, sub-modules, etc. Thus, there is more than one place to search.

To get all actions, you must look for all methods with the action prefix in each controller.

Also, don't forget about attached actions in the actions() controller method. Within the framework, they usually end in Action , look, for example, at rest actions. But no one forces you to call it that, so there is a chance that some external actions can only have a different naming convention (for example, if you work in a team and donโ€™t follow this convention).

And you probably need to exclude folders like the provider .

So this is not a trivial task, but perhaps with some inaccuracies. I just donโ€™t understand what is the point of this.

0
source

I started with Andreas Hinderberger's answer and just set it up. I got something like this:

It uses FileHelper to get all files recursively, which is useful if you extend controllers from base classes. It also formats id-id-id-action-id using Inflector::camel2id to match your routes.

 public function getAllControllerActions() { $controllers = \yii\helpers\FileHelper::findFiles(Yii::getAlias('@app/controllers'), ['recursive' => true]); $actions = []; foreach ($controllers as $controller) { $contents = file_get_contents($controller); $controllerId = Inflector::camel2id(substr(basename($controller), 0, -14)); preg_match_all('/public function action(\w+?)\(/', $contents, $result); foreach ($result[1] as $action) { $actionId = Inflector::camel2id($action); $route = $controllerId . '/' . $actionId; $actions[$route] = $route; } } asort($actions); return $actions; } 
0
source

Follow the example of the passage of all modules and collect all the actions of the controller of the controller (not tested):

 <?php $controllerDirs = []; $controllerDirs[] = \Yii::getAlias('@app/controllers'); if ($commonControllerDir = \Yii::getAlias('@common/controllers', false)) { $controllerDirs['common'] = $commonControllerDir; } foreach (\Yii::$app->modules as $moduleId => $module) { /* * get module base path */ if (method_exists($module, 'getBasePath')) { $basePath = $module->getBasePath(); } else { $reflector = new \ReflectionClass($module['class']); $basePath = StringHelper::dirname($reflector->getFileName()); } $basePath .= '/controllers'; $controllerDirs[$moduleId] = $basePath; } $actions = []; foreach ($controllerDirs as $moduleId => $cDir) { $actions[$moduleId][$cDir] = actionGetcontrollersandactions($cDir); } print_r($actions); function actionGetcontrollersandactions($controllerDir) { $controllerlist = []; if ($handle = opendir($controllerDir)) { 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($controllerDir . '/' . $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; } 
0
source

Source: https://habr.com/ru/post/1210943/


All Articles