列出 Cakephp 3 中的所有控制器/动作 [英] List all controllers/actions in Cakephp 3
本文介绍了列出 Cakephp 3 中的所有控制器/动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何列出我网站上的所有控制器/操作?Configure::listObjects('model') 似乎不再存在.我正在尝试编写一个函数来生成/添加到我的 ACL 设置中的 ACO.谢谢.
How do I list all the controllers/actions on my site? Configure::listObjects('model') doesnt seem to exist anymore. I am trying to write a function to generate/add to the ACO's in my ACL setup. Thanks.
推荐答案
这就是我所做的.在我的资源控制器中:
So here is what I did. In my Resource Controller:
包含反射类/方法库
use ReflectionClass;
use ReflectionMethod;
获取控制器:
public function getControllers() {
$files = scandir('../src/Controller/');
$results = [];
$ignoreList = [
'.',
'..',
'Component',
'AppController.php',
];
foreach($files as $file){
if(!in_array($file, $ignoreList)) {
$controller = explode('.', $file)[0];
array_push($results, str_replace('Controller', '', $controller));
}
}
return $results;
}
现在是行动:
public function getActions($controllerName) {
$className = 'App\Controller\'.$controllerName.'Controller';
$class = new ReflectionClass($className);
$actions = $class->getMethods(ReflectionMethod::IS_PUBLIC);
$results = [$controllerName => []];
$ignoreList = ['beforeFilter', 'afterFilter', 'initialize'];
foreach($actions as $action){
if($action->class == $className && !in_array($action->name, $ignoreList)){
array_push($results[$controllerName], $action->name);
}
}
return $results;
}
最后,将两者联系在一起:
Finally, to tie them boths together:
public function getResources(){
$controllers = $this->getControllers();
$resources = [];
foreach($controllers as $controller){
$actions = $this->getActions($controller);
array_push($resources, $actions);
}
return $resources;
}
希望能帮到一些人.
这篇关于列出 Cakephp 3 中的所有控制器/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文